spec/front_end/parser_spec.rb in loxxy-0.0.11 vs spec/front_end/parser_spec.rb in loxxy-0.0.12

- old
+ new

@@ -35,11 +35,11 @@ context 'Parsing blank files:' do def check_empty_input_result(aParseTree) # Parse results MUST to comply to grammar rule: # program => declaration_star EOF # where the declaration_star MUST be empty - expect(aParseTree.root.symbol.name).to eq('EOF') + expect(aParseTree.root).to be_kind_of(Ast::LoxNoopExpr) end it 'should cope with an empty input' do ptree = subject.parse('') check_empty_input_result(ptree) @@ -62,28 +62,28 @@ context 'Parsing literals:' do it 'should parse a false literal' do input = 'false;' ptree = subject.parse(input) - leaf = walk_subnodes(ptree.root, [0, 0]) + leaf = ptree.root.subnodes[0] expect(leaf).to be_kind_of(Ast::LoxLiteralExpr) expect(leaf.literal).to be_equal(Datatype::False.instance) end it 'should parse a true literal' do input = 'true;' ptree = subject.parse(input) - leaf = walk_subnodes(ptree.root, [0, 0]) + leaf = ptree.root.subnodes[0] expect(leaf).to be_kind_of(Ast::LoxLiteralExpr) expect(leaf.literal).to be_equal(Datatype::True.instance) end it 'should parse number literals' do inputs = %w[1234; 12.34;] inputs.each do |source| ptree = subject.parse(source) - leaf = walk_subnodes(ptree.root, [0, 0]) + leaf = ptree.root.subnodes[0] expect(leaf).to be_kind_of(Ast::LoxLiteralExpr) expect(leaf.literal).to be_kind_of(Datatype::Number) expect(leaf.literal.value).to eq(source.to_f) end end @@ -94,21 +94,21 @@ '"";', '"123";' ] inputs.each do |source| ptree = subject.parse(source) - leaf = walk_subnodes(ptree.root, [0, 0]) + leaf = ptree.root.subnodes[0] expect(leaf).to be_kind_of(Ast::LoxLiteralExpr) expect(leaf.literal).to be_kind_of(Datatype::LXString) expect(leaf.literal.value).to eq(source.gsub(/(^")|(";$)/, '')) end end it 'should parse a nil literal' do input = 'nil;' ptree = subject.parse(input) - leaf = walk_subnodes(ptree.root, [0, 0]) + leaf = ptree.root.subnodes[0] expect(leaf).to be_kind_of(Ast::LoxLiteralExpr) expect(leaf.literal).to be_equal(Datatype::Nil.instance) end end @@ -117,33 +117,23 @@ program = <<-LOX_END // Your first Lox program! print "Hello, world!"; LOX_END ptree = subject.parse(program) - root = ptree.root - expect(root.symbol.name).to eq('program') - (prnt_stmt, eof) = root.subnodes - expect(prnt_stmt).to be_kind_of(Rley::PTree::NonTerminalNode) - expect(prnt_stmt.symbol.name).to eq('printStmt') - expect(prnt_stmt.subnodes.size).to eq(3) - expect(prnt_stmt.subnodes[0]).to be_kind_of(Rley::PTree::TerminalNode) - expect(prnt_stmt.subnodes[0].symbol.name).to eq('PRINT') - expect(prnt_stmt.subnodes[1]).to be_kind_of(Loxxy::Ast::LoxLiteralExpr) - expect(prnt_stmt.subnodes[1].literal).to be_kind_of(Loxxy::Datatype::LXString) - expect(prnt_stmt.subnodes[1].literal.value).to eq('Hello, world!') - expect(prnt_stmt.subnodes[2]).to be_kind_of(Rley::PTree::TerminalNode) - expect(prnt_stmt.subnodes[2].symbol.name).to eq('SEMICOLON') - expect(eof).to be_kind_of(Rley::PTree::TerminalNode) - expect(eof.symbol.name).to eq('EOF') + prnt_stmt = ptree.root + expect(prnt_stmt).to be_kind_of(Ast::LoxPrintStmt) + expect(prnt_stmt.subnodes[0]).to be_kind_of(Ast::LoxLiteralExpr) + expect(prnt_stmt.subnodes[0].literal).to be_kind_of(Loxxy::Datatype::LXString) + expect(prnt_stmt.subnodes[0].literal.value).to eq('Hello, world!') end end # context context 'Parsing arithmetic operations' do it 'should parse the addition of two number literals' do input = '123 + 456;' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:+) @@ -152,11 +142,11 @@ end it 'should parse the subtraction of two number literals' do input = '4 - 3;' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:-) @@ -165,11 +155,11 @@ end it 'should parse multiple additive operations' do input = '5 + 2 - 3;' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:-) @@ -181,11 +171,11 @@ end it 'should parse the division of two number literals' do input = '8 / 2;' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:/) @@ -194,11 +184,11 @@ end it 'should parse the product of two number literals' do input = '12.34 * 0.3;' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:*) @@ -207,11 +197,11 @@ end it 'should parse multiple multiplicative operations' do input = '5 * 2 / 3;' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:/) @@ -223,11 +213,11 @@ end it 'should parse combination of terms and factors' do input = '5 + 2 / 3;' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:+) @@ -241,11 +231,11 @@ context 'Parsing string concatenation' do it 'should parse the concatenation of two string literals' do input = '"Lo" + "ve";' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:+) @@ -257,11 +247,11 @@ context 'Parsing comparison expressions' do it 'should parse the comparison of two number literals' do %w[> >= < <=].each do |predicate| input = "3 #{predicate} 2;" ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(predicate.to_sym) @@ -274,11 +264,11 @@ context 'Parsing equality expressions' do it 'should parse the equality of two number literals' do %w[!= ==].each do |predicate| input = "3 #{predicate} 2;" ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(predicate.to_sym) @@ -288,11 +278,11 @@ end it 'should parse combination of equality expressions' do input = '5 != 2 == false; // A bit contrived example' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:==) @@ -307,11 +297,11 @@ context 'Parsing logical expressions' do it 'should parse the logical operations betweentwo sub-expression' do %w[or and].each do |connector| input = "5 > 2 #{connector} 3 <= 4;" ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(connector.to_sym) @@ -327,10 +317,10 @@ end it 'should parse a combinations of logical expressions' do input = '4 > 3 and 1 < 2 or 4 >= 5;' ptree = subject.parse(input) - parent = ptree.root.subnodes[0] + parent = ptree.root expect(parent).to be_kind_of(Rley::PTree::NonTerminalNode) expect(parent.symbol.name).to eq('exprStmt') expr = parent.subnodes[0] expect(expr).to be_kind_of(Ast::LoxBinaryExpr) expect(expr.operator).to eq(:or) # or has lower precedence than and