add module names and instance variables as parse rules

This commit is contained in:
Torsten Ruger
2014-05-30 12:06:42 +03:00
parent 3e93517986
commit f03d445f3d
4 changed files with 42 additions and 1 deletions

View File

@ -42,6 +42,11 @@ module Ast
end
end
class VariableExpression < NameExpression
end
class ModuleName < NameExpression
end
class StringExpression < Expression
attr_reader :string
def initialize str

View File

@ -25,7 +25,13 @@ module Parser
rule(:exponent) { (str('e')| str('E')) }
# identifier must start with lower case
rule(:name) { keyword.absent? >> (match['a-z'] >> match['a-zA-Z0-9'].repeat).as(:name) >> space? }
# TODO rule forbit names like if_true, because it starts with a keyword. a little looser please!
rule(:name) { keyword.absent? >> (match['a-z_'] >> match['a-zA-Z0-9_'].repeat).as(:name) >> space? }
# instance variables must have the @
rule(:instance_variable) { (match('@') >> name).as(:instance_variable) }
# and class/module names must start with capital
# (admittatly the rule matches constants too, but one step at a time)
rule(:module_name) { keyword.absent? >> (match['A-Z'] >> match['a-zA-Z0-9_'].repeat).as(:module_name) >> space? }
rule(:escape) { str('\\') >> any.as(:esc) }
rule(:string) { quote >> (

View File

@ -9,6 +9,8 @@ module Parser
rule(:integer => simple(:value)) { Ast::IntegerExpression.new(value.to_i) }
rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) }
rule(:instance_variable => simple(:instance_variable)) { Ast::VariableExpression.new(instance_variable.name) }
rule(:module_name => simple(:module_name)) { Ast::ModuleName.new(module_name.to_s) }
rule(:array_constant => sequence(:array_constant) ) { Ast::ArrayExpression.new(array_constant) }
rule(:array_element => simple(:array_element)) { array_element }