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
include Parslet
rule(:conditional) do
keyword_if >> left_parenthesis >> expression.as(:conditional) >> right_parenthesis >> newline >>
expressions_else.as(:if_true) >> expressions_end.as(:if_false)
keyword_if >> left_parenthesis >> (simple_expression|operator_expression).as(:conditional) >> right_parenthesis >> newline >>
expressions_else.as(:if_true) >> newline >> expressions_end.as(:if_false)
end
rule(:while_do) do
keyword_while >> left_parenthesis >> expression.as(:while_cond) >> right_parenthesis >>
keyword_do >> newline >>
keyword_while >> left_parenthesis >> (simple_expression|operator_expression).as(:while_cond) >>
right_parenthesis >> keyword_do >> newline >>
expressions_end.as(:body)
end
end

View File

@ -3,10 +3,11 @@ module Parser
include Parslet
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 )
( (delimit.absent? >> (operator_expression | expression)).repeat(1)).as(:expressions) >> delimit >> newline.maybe
( (delimit.absent? >> expression).repeat(1)).as(:expressions) >> delimit
end
rule(:expressions_do) { delimited_expressions(keyword_do) }

View File

@ -3,7 +3,7 @@ module Parser
include Parslet
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) {

View File

@ -23,7 +23,7 @@ module Parser
#infix doing the heavy lifting here,
# 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] ,
[multiply, 120, :left] ,
[plus, 110, :left],

View File

@ -33,10 +33,10 @@ module Parser
rule(:parmeter => simple(:parmeter)) { parmeter }
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),
: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
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do

View File

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

View File

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

View File

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

View File

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

View File

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