newline fiddling in the parser yields goods results

This commit is contained in:
Torsten Ruger 2014-05-12 13:47:47 +03:00
parent ee4d8033af
commit 956d1eb135
10 changed files with 69 additions and 44 deletions

View File

@ -2,13 +2,13 @@ module Parser
module Control module Control
include Parslet include Parslet
rule(:conditional) do rule(:conditional) do
keyword_if >> left_parenthesis >> expression.as(:conditional) >> right_parenthesis >> newline >> keyword_if >> left_parenthesis >> (simple_expression|operator_expression).as(:conditional) >> right_parenthesis >> newline >>
expressions_else.as(:if_true) >> expressions_end.as(:if_false) expressions_else.as(:if_true) >> newline >> expressions_end.as(:if_false)
end end
rule(:while_do) do rule(:while_do) do
keyword_while >> left_parenthesis >> expression.as(:while_cond) >> right_parenthesis >> keyword_while >> left_parenthesis >> (simple_expression|operator_expression).as(:while_cond) >>
keyword_do >> newline >> right_parenthesis >> keyword_do >> newline >>
expressions_end.as(:body) expressions_end.as(:body)
end end
end end

View File

@ -3,10 +3,11 @@ module Parser
include Parslet include Parslet
rule(:simple_expression) { function_call | integer | string | name } rule(:simple_expression) { function_call | integer | string | name }
rule(:expression) { (while_do | conditional | simple_expression ) >> newline.maybe }
rule(:expression) { (while_do | conditional | operator_expression | function_call ) >> newline }
def delimited_expressions( delimit ) def delimited_expressions( delimit )
( (delimit.absent? >> (operator_expression | expression)).repeat(1)).as(:expressions) >> delimit >> newline.maybe ( (delimit.absent? >> expression).repeat(1)).as(:expressions) >> delimit
end end
rule(:expressions_do) { delimited_expressions(keyword_do) } rule(:expressions_do) { delimited_expressions(keyword_do) }

View File

@ -3,7 +3,7 @@ module Parser
include Parslet include Parslet
rule(:function_definition) { rule(:function_definition) {
keyword_def >> name.as(:function_definition) >> parmeter_list >> newline >> expressions_end keyword_def >> name.as(:function_name) >> parmeter_list >> newline >> expressions_end >> newline
} }
rule(:parmeter_list) { rule(:parmeter_list) {

View File

@ -23,7 +23,7 @@ module Parser
#infix doing the heavy lifting here, #infix doing the heavy lifting here,
# is defined as an expressions and array of [atoms,priority,binding] triples # is defined as an expressions and array of [atoms,priority,binding] triples
rule(:operator_expression) do infix_expression(expression, rule(:operator_expression) do infix_expression(simple_expression,
[exponent, 120, :left] , [exponent, 120, :left] ,
[multiply, 120, :left] , [multiply, 120, :left] ,
[plus, 110, :left], [plus, 110, :left],

View File

@ -33,10 +33,10 @@ module Parser
rule(:parmeter => simple(:parmeter)) { parmeter } rule(:parmeter => simple(:parmeter)) { parmeter }
rule(:parmeter_list => sequence(:parmeter_list)) { parmeter_list } rule(:parmeter_list => sequence(:parmeter_list)) { parmeter_list }
rule(:function_definition => simple(:function_definition), rule(:function_name => simple(:function_name),
:parmeter_list => sequence(:parmeter_list), :parmeter_list => sequence(:parmeter_list),
:expressions => sequence(:expressions) , :end => simple(:e)) do :expressions => sequence(:expressions) , :end => simple(:e)) do
Ast::FunctionExpression.new(function_definition.name, parmeter_list, expressions) Ast::FunctionExpression.new(function_name.name, parmeter_list, expressions)
end end
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do

View File

@ -12,6 +12,7 @@ else
667 667
end end
HERE HERE
@string_input.chop!
@parse_output = {:if=>"if", :conditional=>{:integer=>"0"}, @parse_output = {:if=>"if", :conditional=>{:integer=>"0"},
:if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"}, :if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"},
:if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}} :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}

View File

@ -6,29 +6,32 @@ class TestExpressions < MiniTest::Test
def test_expression_else def test_expression_else
@string_input = <<HERE @string_input = <<HERE
4 dud
5 fuu(3)
else else
HERE HERE
@parse_output = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}], :else=>"else"} @string_input.chop!
@transform_output = {:expressions=>[ Ast::IntegerExpression.new(4), Ast::IntegerExpression.new(5)], :else=>"else"} @parse_output = {:expressions=>[{:name=>"dud"},
{:function_call=>{:name=>"fuu"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}],
:else=>"else"}
@transform_output ={:expressions=>[Ast::NameExpression.new("dud"),
Ast::FuncallExpression.new("fuu", [Ast::IntegerExpression.new(3)] )], :else=>"else"}
@parser = @parser.expressions_else @parser = @parser.expressions_else
end end
def test_expression_end def test_expression_end
@string_input = <<HERE @string_input = <<HERE
5
name name
call(4,6) call(4,6)
end end
HERE HERE
@parse_output = {:expressions=>[{:integer=>"5"}, {:name=>"name"}, @string_input.chop!
{:function_call=>{:name=>"call"}, :argument_list=>[{:argument=>{:integer=>"4"}}, @parse_output = {:expressions=>[{:name=>"name"},
{:argument=>{:integer=>"6"}}]}], :end=>"end"} {:function_call=>{:name=>"call"}, :argument_list=>[{:argument=>{:integer=>"4"}}, {:argument=>{:integer=>"6"}}]}],
args = [ Ast::IntegerExpression.new(4) , Ast::IntegerExpression.new(6) ] :end=>"end"}
@transform_output = {:expressions=>[ Ast::IntegerExpression.new(5), @transform_output = {:expressions=>[Ast::NameExpression.new("name"),
Ast::NameExpression.new("name") , Ast::FuncallExpression.new("call", [Ast::IntegerExpression.new(4),Ast::IntegerExpression.new(6)] )],
Ast::FuncallExpression.new("call", args ) ] , :end=>"end"} :end=>"end"}
@parser = @parser.expressions_end @parser = @parser.expressions_end
end end

View File

@ -10,7 +10,7 @@ def foo(x)
5 5
end end
HERE HERE
@parse_output = {:function_definition=>{:name=>"foo"}, @parse_output ={:function_name=>{:name=>"foo"},
:parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:integer=>"5"}], :end=>"end"} :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:integer=>"5"}], :end=>"end"}
@transform_output = Ast::FunctionExpression.new('foo', @transform_output = Ast::FunctionExpression.new('foo',
[Ast::NameExpression.new('x')], [Ast::NameExpression.new('x')],
@ -18,16 +18,21 @@ HERE
@parser = @parser.function_definition @parser = @parser.function_definition
end end
def test_function_assignment def test_function_ops
@string_input = <<HERE @string_input = <<HERE
def foo(x) def foo(x)
abba = 5 abba = 5
2 + 5
end end
HERE HERE
@parse_output = {:function_definition=>{:name=>"foo"}, @parse_output = {:function_name=>{:name=>"foo"},
:parmeter_list=>[{:parmeter=>{:name=>"x"}}], :parmeter_list=>[{:parmeter=>{:name=>"x"}}],
:expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"} :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}},
@transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("abba"),Ast::IntegerExpression.new(5))] ) {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}
@transform_output = Ast::FunctionExpression.new(:foo,
[Ast::NameExpression.new("x")] ,
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("abba"),Ast::IntegerExpression.new(5)),
Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )
@parser = @parser.function_definition @parser = @parser.function_definition
end end
@ -35,23 +40,24 @@ HERE
@string_input = <<HERE @string_input = <<HERE
def ofthen(n) def ofthen(n)
if(0) if(0)
42 isit = 42
else else
667 maybenot = 667
end end
end end
HERE HERE
@parse_output = {:function_definition=>{:name=>"ofthen"}, @parse_output = {:function_name=>{:name=>"ofthen"},
:parmeter_list=>[{:parmeter=>{:name=>"n"}}], :parmeter_list=>[{:parmeter=>{:name=>"n"}}],
:expressions=>[ :expressions=>[{:if=>"if", :conditional=>{:integer=>"0"},
{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:l=>{:name=>"isit"}, :o=>"= ", :r=>{:integer=>"42"}}], :else=>"else"},
:if_true=>{:expressions=>[{:integer=>"42"}], :if_false=>{:expressions=>[{:l=>{:name=>"maybenot"}, :o=>"= ", :r=>{:integer=>"667"}}], :end=>"end"}}],
:else=>"else"}, :end=>"end"}
:if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}], :end=>"end"}
@transform_output = Ast::FunctionExpression.new(:ofthen, @transform_output = Ast::FunctionExpression.new(:ofthen,
[Ast::NameExpression.new("n")] , [Ast::NameExpression.new("n")] ,
[Ast::ConditionalExpression.new(Ast::IntegerExpression.new(0), [Ast::ConditionalExpression.new(Ast::IntegerExpression.new(0),
[Ast::IntegerExpression.new(42)],[Ast::IntegerExpression.new(667)] )] ) [Ast::OperatorExpression.new("=", Ast::NameExpression.new("isit"),
Ast::IntegerExpression.new(42))],
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("maybenot"),Ast::IntegerExpression.new(667))] )] )
@parser = @parser.function_definition @parser = @parser.function_definition
end end
@ -59,12 +65,26 @@ HERE
@string_input = <<HERE @string_input = <<HERE
def fibonaccit(n) def fibonaccit(n)
a = 0 a = 0
while n do while (n) do
1 some = 43
other = some * 4
end
end end
HERE HERE
@parse_output = nil @parse_output ={:function_name=>{:name=>"fibonaccit"},
@transform_output = nil :parmeter_list=>[{:parmeter=>{:name=>"n"}}],
:expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}},
{:while=>"while", :while_cond=>{:name=>"n"}, :do=>"do",
:body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}},
{:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}],
:end=>"end"}}],
:end=>"end"}
@transform_output = Ast::FunctionExpression.new(:fibonaccit,
[Ast::NameExpression.new("n")] ,
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(0)),
Ast::WhileExpression.new(Ast::NameExpression.new("n"),
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(43)),
Ast::OperatorExpression.new("=", Ast::NameExpression.new("other"),Ast::OperatorExpression.new("*", Ast::NameExpression.new("some"),Ast::IntegerExpression.new(4)))] )] )
@parser = @parser.function_definition @parser = @parser.function_definition
end end
end end

View File

@ -12,7 +12,7 @@ end
foo( 3 ) foo( 3 )
HERE HERE
@parse_output = [{:function_definition=>{:name=>"foo"}, @parse_output = [{:function_name=>{:name=>"foo"},
:parmeter_list=>[{:parmeter=>{:name=>"x"}}], :parmeter_list=>[{:parmeter=>{:name=>"x"}}],
:expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}, :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"},
{:function_call=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}] {:function_call=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}]

View File

@ -11,7 +11,7 @@ while(1) do
puts(b) puts(b)
end end
HERE HERE
@string_input.chop!
@parse_output = {:while=>"while", @parse_output = {:while=>"while",
:while_cond=>{:integer=>"1"}, :while_cond=>{:integer=>"1"},
:do=>"do", :do=>"do",