Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
113 lines (96 sloc) 1.98 KB
grammar Orange
rule script
expression more_expressions:(newline expression)* <Script> {
def expressions
[expression] + more_expressions.elements.map { |e| e.expression }
end
}
end
rule expression
spaced_statements:(space statement)+ space <Expression> {
def statements
spaced_statements.elements.map { |e| e.statement }
end
}
/
space
end
rule statement
cmp / assign / call / equation / if / def / object / comment
end
rule object
number / string / var
end
rule call
func:var arglist <Call>
end
rule def
"def" space func:var arglist space newline
expressions:(expression newline)* space
"end" <Def> {
def expressions
super.elements.map { |e| e.expression }
end
}
end
rule if
"if" space condition:expression space newline
expressions:(expression newline)* space
"end" <If> {
def expressions
super.elements.map { |e| e.expression }
end
}
end
rule cmp
object space op:("&&" / "||" / "==" / "!=" / ">" / "<") space expression
end
# Fix operator precedance
rule equation
object space op:[\+\-\/\*] space expression
end
rule arglist
"(" arg:statement more_args:(space "," space arg:statement)* ")" {
def args
[arg] + more_args.elements.map { |e| e.arg }
end
} /
"()" {
def args
[]
end
}
end
rule assign
var space "=" !"=" expression <Assign>
end
rule var
!keyword [a-z] [\w]* <Var>
end
rule keyword
"def" / "end"
end
rule number
[0-9]+ <Number> {
def value
text_value.to_i
end
}
end
rule string
'"' (!'"' . / '\"')* '"' <String> {
def value
text_value.gsub('"', '')
end
}
end
rule comment
'#' ( !newline . )+ ( newline / !. )
end
rule space
[ \t]*
end
rule newline
space ("\r\n"+ / [\r\n]+ / ";") space
end
end
You can’t perform that action at this time.