Class: Parser::ParserRule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, name, elements, params, block) ⇒ ParserRule

Returns a new instance of ParserRule.



46
47
48
49
50
51
52
53
# File 'lib/appswarm/tools/parser/parser_lib.rb', line 46

def initialize(parser,name,elements,params,block)
  @parser=parser
  @name=name
  @elements=[elements].flatten
  
  @params=params
  @block=block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



45
46
47
# File 'lib/appswarm/tools/parser/parser_lib.rb', line 45

def block
  @block
end

Instance Method Details

#parse(ctx) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/appswarm/tools/parser/parser_lib.rb', line 59

def parse(ctx)
  parsed=[]
  @elements.each{|element|
    #puts "TRY #{element.inspect} on '#{ctx.str}'"
    case element
      when Regexp
        regex=element.source
        str=ctx.str
        #pp "R:",regex,element,element.class
        #$KCODE="n"
        regex= Regexp.new("\\A#{regex}",0,'n') ##/^#{regex}/
        #regex=element
        #pp "REGEX:",regex
        match=str.match(regex)
        unless match
         # puts "FAIL at regexp #{element}"
          return nil
        end
        rstr=match[0]

        #pp "str:",str
        #str.each_byte{|b|pp b}
        #pp "RESULT:",match,rstr,regex
        return nil unless rstr
        ctx.pos+=rstr.length
        
        parsed<<ParsedElement.new("Regexp",rstr,nil)
      when Symbol
        value=@parser.parseContext(ctx,element)
        if value.nil?
          #puts "FAIL at symbol"
          return nil
        end
        parsed<<value
      when String
        if ctx.str[0..(element.length-1)]!=element
          #puts ctx.str
          #puts "Fail at string"
          return nil
        end
        
        ctx.pos+=element.length
        parsed<<ParsedElement.new("String",element,nil)
      else
        raise "Unknown Element !!!! #{element}"
    end
  }
  
  return ParsedElement.new(@name,parsed,@block)
end

#to_sObject



55
56
57
# File 'lib/appswarm/tools/parser/parser_lib.rb', line 55

def to_s
  "#{@name}=>#{@elements}"
end