Skip to content

Commit

Permalink
Revert changes to old Parser and Lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Oct 14, 2023
1 parent 8b4fe8c commit 5cd38d4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 138 deletions.
9 changes: 2 additions & 7 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/lrama/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions spec/lrama/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions spec/lrama/counterexamples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
93 changes: 5 additions & 88 deletions spec/lrama/lexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand All @@ -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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
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 <stdio.h>\n", 2],
[" #include <stdlib.h>\n", 3],
[" #include <ctype.h>\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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
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 <stdio.h>\n", 2],
[" #include <stdlib.h>\n", 3],
[" #include <ctype.h>\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
Loading

0 comments on commit 5cd38d4

Please sign in to comment.