Skip to content

Commit

Permalink
Add NewPerser support
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Oct 6, 2023
1 parent 5db431c commit 0e0343a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/lrama/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def run(argv)
warning = Lrama::Warning.new
text = options.y.read
options.y.close if options.y != STDIN
grammar = Lrama::NewParser.new(text).parse
grammar = Lrama::NewParser.new(text, options.header_file).parse
states = Lrama::States.new(grammar, warning, trace_state: (options.trace_opts[:automaton] || options.trace_opts[:closure]))
states.compute
context = Lrama::Context.new(states)
Expand Down
13 changes: 11 additions & 2 deletions lib/lrama/new_parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ rule
prologue_declarations: # empty
| prologue_declarations prologue_declaration

prologue_declaration: "%{" {@lexer.status = :c_declaration; @lexer.end_symbol = '%}'; @grammar.prologue_first_lineno = @lexer.line} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "%}" { @grammar.prologue = val[2] }
prologue_declaration: "%{" {@lexer.status = :c_declaration; @lexer.end_symbol = '%}'; @grammar.prologue_first_lineno = @lexer.line} C_DECLARATION {@lexer.status = :initial; @lexer.end_symbol = nil} "%}" { @grammar.prologue = prologue(val[2]) }
| "%require" STRING

bison_declarations: /* empty */ { result = "" }
Expand Down Expand Up @@ -119,8 +119,9 @@ end

---- inner

def initialize(text)
def initialize(text, header_file)
@text = text
@header_path = header_file ? header_file.sub("./", "") : nil
@lineno = []
@column = []
end
Expand Down Expand Up @@ -150,3 +151,11 @@ def build_token(type:, s_value:, line:, column:, alias_name: nil)

token
end

def prologue(s_value)
if @header_path
"\n#include \"#{@header_path}\"\n" + s_value
else
s_value
end
end

0 comments on commit 0e0343a

Please sign in to comment.