Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/tools/parser/parser_lib.rb

Defined Under Namespace

Classes: ParsedElement, ParserContext, ParserRule

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Parser

Returns a new instance of Parser.



112
113
114
115
116
117
118
119
120
# File 'lib/appswarm/tools/parser/parser_lib.rb', line 112

def initialize(name,&block)
  @name=name
  @block=block
  @rules={}
  @root=nil

  instance_eval(&block)

end

Instance Method Details

#parse(inString) ⇒ Object



135
136
137
138
# File 'lib/appswarm/tools/parser/parser_lib.rb', line 135

def parse(inString)
  ctx=ParserContext.new(inString,0)
  parseContext(ctx)
end

#parseContext(ctx, element = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/appswarm/tools/parser/parser_lib.rb', line 141

def parseContext(ctx,element=nil)
  element||=@root
  rules=@rules[element]

  raise "No rule found for #{element}" unless rules
  result=nil
  rules.each{|rule|
    nctx=ctx.dup
#      pp "TRYING #{rule}"
    result=rule.parse(nctx)
    if result
      ctx.pos=nctx.pos
      result.block=rule.block
#        pp "OK #{rule}"
      return result
    end
#      pp "FAILED #{rule}"
  }
  result
end

#rule(hash, *params, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/appswarm/tools/parser/parser_lib.rb', line 122

def rule(hash,*params,&block)
  name=hash.keys[0]
  elements=hash[name]
  

  @rules[name]||=[]
  rule=ParserRule.new(self,name,elements,params,block)
  @rules[name]<<rule
  @root||=name
  rule
end