diff --git a/lib/lrama/lexer.rb b/lib/lrama/lexer.rb index 5c8edae9..84c62205 100644 --- a/lib/lrama/lexer.rb +++ b/lib/lrama/lexer.rb @@ -21,9 +21,8 @@ class Lexer attr_reader :prologue, :bison_declarations, :grammar_rules, :epilogue, :bison_declarations_tokens, :grammar_rules_tokens - def initialize(text, header_file) + def initialize(text) @text = text - @header_path = header_file ? header_file.sub("./", "") : nil @state = Initial # Array of texts @prologue = [] @@ -61,11 +60,7 @@ def lex_text # Skip until "%{" if string == "%{\n" @state = Prologue - if @header_path - @prologue << ["#include \"#{@header_path}\"\n", lineno] - else - @prologue << ["", lineno] - end + @prologue << ["", lineno] next end when Prologue diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index be4642c0..18e2ef03 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -8,14 +8,13 @@ class Parser T = Lrama::Lexer::Token - def initialize(text, header_file) + def initialize(text) @text = text - @header_file = header_file end def parse report_duration(:parse) do - lexer = Lexer.new(@text, @header_file) + lexer = Lexer.new(@text) grammar = Grammar.new process_prologue(grammar, lexer) parse_bison_declarations(TokenScanner.new(lexer.bison_declarations_tokens), grammar) diff --git a/spec/lrama/context_spec.rb b/spec/lrama/context_spec.rb index dfc62452..a8ff640d 100644 --- a/spec/lrama/context_spec.rb +++ b/spec/lrama/context_spec.rb @@ -5,7 +5,7 @@ describe "basic" do it do y = File.read(fixture_path("context/basic.y")) - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute context = Lrama::Context.new(states) @@ -181,7 +181,7 @@ %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute context = Lrama::Context.new(states) @@ -230,7 +230,7 @@ %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute context = Lrama::Context.new(states) diff --git a/spec/lrama/counterexamples_spec.rb b/spec/lrama/counterexamples_spec.rb index 11e32684..f0cce980 100644 --- a/spec/lrama/counterexamples_spec.rb +++ b/spec/lrama/counterexamples_spec.rb @@ -50,7 +50,7 @@ end it "build counterexamples of S/R conflicts" do - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute counterexamples = Lrama::Counterexamples.new(states) @@ -249,7 +249,7 @@ end it "build counterexamples of R/R conflicts" do - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute counterexamples = Lrama::Counterexamples.new(states) @@ -326,7 +326,7 @@ end it "build counterexamples of S/R conflicts" do - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute counterexamples = Lrama::Counterexamples.new(states) @@ -407,7 +407,7 @@ end it "build counterexamples of S/R and R/R conflicts" do - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute counterexamples = Lrama::Counterexamples.new(states) diff --git a/spec/lrama/lexer_spec.rb b/spec/lrama/lexer_spec.rb index a6324665..5bdc2a04 100644 --- a/spec/lrama/lexer_spec.rb +++ b/spec/lrama/lexer_spec.rb @@ -4,7 +4,7 @@ describe '#lex' do it "basic" do y = File.read(fixture_path("common/basic.y")) - lexer = Lrama::Lexer.new(y, nil) + lexer = Lrama::Lexer.new(y) expect(lexer.prologue.first[1]).to eq(7) expect(lexer.prologue.map(&:first).join).to eq(<<~TEXT) @@ -269,7 +269,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS it "nullable" do y = File.read(fixture_path("common/nullable.y")) - lexer = Lrama::Lexer.new(y, nil) + lexer = Lrama::Lexer.new(y) expect(lexer.grammar_rules_tokens).to eq([ T.new(type: T::Ident_Colon, s_value: "program"), @@ -335,7 +335,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS ; %% INPUT - lexer = Lrama::Lexer.new(y, nil) + lexer = Lrama::Lexer.new(y) user_codes = lexer.grammar_rules_tokens.select do |t| t.type == T::User_code end @@ -389,7 +389,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS ; %% INPUT - lexer = Lrama::Lexer.new(y, nil) + lexer = Lrama::Lexer.new(y) expect(lexer.grammar_rules_tokens).to eq([ T.new(type: T::Ident_Colon, s_value: "line"), @@ -457,7 +457,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS ; %% INPUT - lexer = Lrama::Lexer.new(y, nil) + lexer = Lrama::Lexer.new(y) user_codes = lexer.grammar_rules_tokens.select do |t| t.type == T::User_code end @@ -475,88 +475,5 @@ class : keyword_class tSTRING keyword_end %prec tPLUS expect(user_codes.map(&:s_value)).to eq([expected]) end end - - describe "include header" do - context "when header is specified" do - it "parses correctly" do - y = <<~INPUT -%{ - #include - #include - #include - - static int yylex(YYSTYPE *val, YYLTYPE *loc); - static int yyerror(YYLTYPE *loc, const char *str); -%} - -%union { - int i; -} - -%% - -program : expr - ; - -%% - INPUT - - lexer = Lrama::Lexer.new(y, "./test.h") - expect(lexer.prologue).to eq( - [ - ["#include \"test.h\"\n", 1], - [" #include \n", 2], - [" #include \n", 3], - [" #include \n", 4], - ["\n", 5], - [" static int yylex(YYSTYPE *val, YYLTYPE *loc);\n", 6], - [" static int yyerror(YYLTYPE *loc, const char *str);\n", 7], - ["", 8] - ] - ) - end - end - - context "when header is not specified" do - context "when header is specified" do - it "parses correctly" do - y = <<~INPUT -%{ - #include - #include - #include - - static int yylex(YYSTYPE *val, YYLTYPE *loc); - static int yyerror(YYLTYPE *loc, const char *str); -%} - -%union { - int i; -} - -%% - -program : expr - ; - -%% - INPUT - lexer = Lrama::Lexer.new(y, nil) - expect(lexer.prologue).to eq( - [ - ["", 1], - [" #include \n", 2], - [" #include \n", 3], - [" #include \n", 4], - ["\n", 5], - [" static int yylex(YYSTYPE *val, YYLTYPE *loc);\n", 6], - [" static int yyerror(YYLTYPE *loc, const char *str);\n", 7], - ["", 8] - ] - ) - end - end - end - end end end diff --git a/spec/lrama/parser_spec.rb b/spec/lrama/parser_spec.rb index 7b28a164..6bae8ace 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -41,7 +41,7 @@ describe '#parse' do it "basic" do y = File.read(fixture_path("common/basic.y")) - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.union.code.s_value).to eq(<<-CODE.chomp) @@ -408,7 +408,7 @@ it "nullable" do y = File.read(fixture_path("common/nullable.y")) - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.nterms.sort_by(&:number)).to eq([ Sym.new(id: T.new(type: T::Ident, s_value: "$accept"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 0, nullable: false), @@ -562,7 +562,7 @@ class : keyword_class tSTRING keyword_end { code 1 } %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar._rules).to eq([ [ @@ -605,7 +605,7 @@ class : keyword_class tSTRING keyword_end { code 1 } %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.terms.sort_by(&:number)).to eq([ Sym.new(id: T.new(type: T::Ident, s_value: "EOI"), alias_name: "\"EOI\"", number: 0, tag: nil, term: true, token_id: 0, nullable: false, precedence: nil), @@ -661,7 +661,7 @@ class : keyword_class { code 1 } tSTRING { code 2 } keyword_end { code 3 } %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.nterms.sort_by(&:number)).to eq([ Sym.new(id: T.new(type: T::Ident, s_value: "$accept"), alias_name: nil, number: 11, tag: nil, term: false, token_id: 0, nullable: false), @@ -756,7 +756,7 @@ class : keyword_class tSTRING %prec tPLUS keyword_end { code 1 } %% INPUT - parser = Lrama::Parser.new(y, nil) + parser = Lrama::Parser.new(y) expect { parser.parse }.to raise_error("Ident after %prec") end @@ -773,7 +773,7 @@ class : keyword_class { code 2 } tSTRING %prec "=" '!' keyword_end { code 3 } %% INPUT - parser = Lrama::Parser.new(y, nil) + parser = Lrama::Parser.new(y) expect { parser.parse }.to raise_error("Char after %prec") end @@ -790,7 +790,7 @@ class : keyword_class { code 4 } tSTRING '?' keyword_end %prec tEQ { code 5 } { %% INPUT - parser = Lrama::Parser.new(y, nil) + parser = Lrama::Parser.new(y) expect { parser.parse }.to raise_error("Multiple User_code after %prec") end @@ -811,7 +811,7 @@ class : keyword_class %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse codes = grammar.rules.map(&:code).compact expect(codes.count).to eq(1) @@ -838,7 +838,7 @@ class : keyword_class %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse codes = grammar.rules.map(&:code).compact expect(codes.count).to eq(1) @@ -883,7 +883,7 @@ class : keyword_class tSTRING keyword_end { code 1 } %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.terms.sort_by(&:number)).to eq([ Sym.new(id: T.new(type: T::Ident, s_value: "EOI"), alias_name: "\"EOI\"", number: 0, tag: nil, term: true, token_id: 0, nullable: false), @@ -932,7 +932,7 @@ class : keyword_class tSTRING keyword_end { code 1 } %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.terms.sort_by(&:number)).to eq([ Sym.new(id: T.new(type: T::Ident, s_value: "EOI"), alias_name: "\"EOI\"", number: 0, tag: nil, term: true, token_id: 0, nullable: false, precedence: nil), @@ -978,7 +978,7 @@ class : keyword_class tSTRING keyword_end { code 1 } ; %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.rules).to eq([ Rule.new( @@ -1086,7 +1086,7 @@ class : keyword_class tSTRING keyword_end { code 1 } ; %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.rules).to eq([ Rule.new( @@ -1177,7 +1177,7 @@ class : keyword_class tSTRING keyword_end { code 1 } { $$ = $1 - $2; } ; INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse expect(grammar.rules).to eq([ Rule.new( @@ -1311,7 +1311,7 @@ class : keyword_class tSTRING keyword_end { code 1 } ; INPUT - expect { Lrama::Parser.new(y, nil).parse }.to raise_error("'results' is invalid name.") + expect { Lrama::Parser.new(y).parse }.to raise_error("'results' is invalid name.") end end end @@ -1348,7 +1348,7 @@ class : keyword_class tSTRING keyword_end ; %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse terms = grammar.terms.sort_by(&:number).map do |term| [term.id.s_value, term.token_id] end @@ -1397,7 +1397,7 @@ class : keyword_class tSTRING keyword_end %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse codes = grammar.rules.map(&:code) expect(codes.count).to eq(3) @@ -1444,7 +1444,7 @@ class : keyword_class tSTRING keyword_end %% INPUT - expect { Lrama::Parser.new(y, nil).parse }.to raise_error(RuntimeError) do |e| + expect { Lrama::Parser.new(y).parse }.to raise_error(RuntimeError) do |e| expect(e.message).to eq(<<~MSG.chomp) $$ of 'stmt' has no declared type $1 of 'stmt' has no declared type diff --git a/spec/lrama/states_spec.rb b/spec/lrama/states_spec.rb index 6c4ac1ef..e46c3759 100644 --- a/spec/lrama/states_spec.rb +++ b/spec/lrama/states_spec.rb @@ -5,7 +5,7 @@ describe '#compute' do it "basic" do y = File.read(fixture_path("common/basic.y")) - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -298,7 +298,7 @@ class go to state 5 it '#State#accessing_symbol' do y = File.read(fixture_path("common/basic.y")) - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -338,7 +338,7 @@ class go to state 5 describe '#reads_relation' do it do y = File.read(fixture_path("states/reads_relation.y")) - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -605,7 +605,7 @@ class go to state 5 describe '#includes_relation' do it do y = File.read(fixture_path("states/includes_relation.y")) - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -904,7 +904,7 @@ class go to state 5 %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -973,7 +973,7 @@ class go to state 5 %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -1063,7 +1063,7 @@ class go to state 5 %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -1180,7 +1180,7 @@ class go to state 5 %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -1277,7 +1277,7 @@ class go to state 5 %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -1426,7 +1426,7 @@ class go to state 5 %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -1598,7 +1598,7 @@ class go to state 5 %% INPUT - grammar = Lrama::Parser.new(y, nil).parse + grammar = Lrama::Parser.new(y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -1816,7 +1816,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS end it "has errors for r/r conflicts" do - grammar = Lrama::Parser.new(header + y, nil).parse + grammar = Lrama::Parser.new(header + y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -1840,7 +1840,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS end it "has errors for s/r conflicts and r/r conflicts" do - grammar = Lrama::Parser.new(header + y, nil).parse + grammar = Lrama::Parser.new(header + y).parse states = Lrama::States.new(grammar, warning) states.compute @@ -1864,7 +1864,7 @@ class : keyword_class tSTRING keyword_end %prec tPLUS end it "has warns for s/r conflicts and r/r conflicts" do - grammar = Lrama::Parser.new(header + y, nil).parse + grammar = Lrama::Parser.new(header + y).parse states = Lrama::States.new(grammar, warning) states.compute