Skip to content

Commit

Permalink
Merge pull request #954 from otakubeam/peg-faster-set
Browse files Browse the repository at this point in the history
Peg faster set
  • Loading branch information
borisbat authored Feb 9, 2024
2 parents eeec54f + 3e962cc commit f48cd51
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
3 changes: 1 addition & 2 deletions modules/dasPEG/peg/detail/helpers.das
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ module helpers shared
require daslib/fio

def abort(message: string)
print("{message}\n")
false |> stackwalk <| false // God I love the pipe syntax so much
unsafe { exit(1); }
panic(message)


def iota
Expand Down
2 changes: 1 addition & 1 deletion modules/dasPEG/peg/meta_ast.das
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ variant Terminal
lit: string

// Range: set('0'..'9', 'a'..'z')
charset: array<tuple<int; int>>
charset: tuple< chars : bool[256]; intervals : array< tuple<int;int> > >

// Log: allows to send messages via `log("Parsed A: {a}")`
log_msg: ExpressionPtr
Expand Down
31 changes: 18 additions & 13 deletions modules/dasPEG/peg/parse_macro.das
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ require detail/colors
require peg/parser_generator
require peg/meta_ast

require math

// To avoid overflow during template expansion
options stack = 10000000

Expand Down Expand Up @@ -105,7 +107,7 @@ def flatten_intervals ( charset : array<tuple<from:int;to:int>>; invert:bool )
if start != -1
result |> push <| [[auto start, 256]]

return <- result
return <- [[auto chars, result]]

[macro_function]
def into_rule_(var expr: ExpressionPtr): Rule_?
Expand Down Expand Up @@ -224,9 +226,7 @@ def into_rule_(var expr: ExpressionPtr): Rule_?
for arg in set.arguments
terms |> push (arg |> into_terminal)

var fterms <- flatten_intervals(terms, inverting)

return <- new [[Rule_ rule <- [[Rule terminal <- [[Terminal charset <- fterms]] ]] ]]
return <- new [[Rule_ rule <- [[Rule terminal <- [[Terminal charset <- flatten_intervals(terms, inverting)]] ]] ]]

elif expr is ExprCall && (expr as ExprCall).name == "log"
// Log partial matching: log("Here goes your message")
Expand Down Expand Up @@ -351,19 +351,24 @@ class ParseMacro : AstCallMacro

var inscope gen <- [[ParserGenerator id = times_invoked++]]

for mrule in rules_
gram |> emplace(mrule |> transform)
try
for mrule in rules_
gram |> emplace(mrule |> transform)

if failed_to_transform
return <- [[ExpressionPtr]]

if failed_to_transform
return <- [[ExpressionPtr]]
// Generate

// Generate
for mrule in rules_
for opt in mrule.options_
gen |> accept_option(opt) // Set the state of the generator up

for mrule in rules_
for opt in mrule.options_
gen |> accept_option(opt) // Set the state of the generator up
gen |> generate_grammar(gram, name)

gen |> generate_grammar(gram, name)
recover
if failed_to_transform
return <- [[ExpressionPtr]]

// Emit wrapper code
var first_method_name = gram[0].name
Expand Down
18 changes: 7 additions & 11 deletions modules/dasPEG/peg/parser_generator.das
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,8 @@ def describe_terminal(term: Terminal): string

if [[Terminal charset = $v(ranges) ]]
var sb = build_string <| $ ( writer )
for i in range() <| min(ranges |> length, 3) // Don't print too many
var range_ = ranges[i]
for i in range() <| min(ranges.intervals |> length, 3) // Don't print too many
var range_ = ranges.intervals[i]

if range_._0 == range_._1
writer |> write <| " '{range_._0}' "
Expand Down Expand Up @@ -979,15 +979,8 @@ def generate_terminal(var gen: ParserGenerator; var terminal: Terminal): Express
// Get the currect charachter from the stream
var inscope rule_code: array<ExpressionPtr>
rule_code |> append_block_new <| (qmacro_block <| {
let current_char = parser |> get_current_char;
})

for r in ranges // Check in it is inside any of the ranges
rule_code |> append_block_new <| generate_range_check(r)

// Otherwise return false
rule_code |> append_block_new <| (qmacro_block <| {
return false;
let ch = parser |> get_current_char;
return ch != -1 && $v(ranges.chars)[ch];
})

return <- qmacro_block <|
Expand All @@ -996,6 +989,9 @@ def generate_terminal(var gen: ParserGenerator; var terminal: Terminal): Express

if !$i(name)
if $v(gen.tracing)
let ch = parser |> get_current_char
if ch != -1 && (ch < 0 || ch >= 256)
panic("failed character set check {ch}")
parser |> log_fail <| "Failed the range check for character {parser.index}"

if parser.error_reporting && !parser.suppress_errors |> back && parser.index == parser.longest_prefix
Expand Down

0 comments on commit f48cd51

Please sign in to comment.