Class: WMLAction::Parser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/wml_action/lexer.rex.rb,
lib/wml_action/parser.tab.rb

Defined Under Namespace

Classes: ScanError

Constant Summary collapse

OTAG =
/\[(\w+)\]/
CTAG =
/\[\/(\w+)\]/
ATTR =
/(\w+)=/
MACRO =
/\{.+\}/
ANUMBER =
/-?\d+/
ASTR =
/"[^"]*"/
APLAIN =
/.+/
SLASH =
/\//
COMMENT =
/\#.*$/
BLANK =
/[ \t]+/
Racc_arg =
[
racc_action_table,
racc_action_check,
racc_action_default,
racc_action_pointer,
racc_goto_table,
racc_goto_check,
racc_goto_default,
racc_goto_pointer,
racc_nt_base,
racc_reduce_table,
racc_token_table,
racc_shift_n,
racc_reduce_n,
racc_use_result_var ]
Racc_token_to_s_table =
[
"$end",
"error",
"OTAG",
"CTAG",
"MACRO",
"\"+\"",
"\"-\"",
"ATTR",
"APLAIN",
"AMACRO",
"ANUMBER",
"ASTR",
"UNDERSC",
"APLUS",
"SLASH",
"$start",
"target",
"wml_doc",
"tag",
"contents",
"content",
"action",
"attribute",
"filter",
"aop",
"string_val" ]
Racc_debug_parser =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



29
30
31
# File 'lib/wml_action/lexer.rex.rb', line 29

def filename
  @filename
end

#linenoObject

Returns the value of attribute lineno.



28
29
30
# File 'lib/wml_action/lexer.rex.rb', line 28

def lineno
  @lineno
end

#ssObject Also known as: match

Returns the value of attribute ss.



30
31
32
# File 'lib/wml_action/lexer.rex.rb', line 30

def ss
  @ss
end

#stateObject

Returns the value of attribute state.



31
32
33
# File 'lib/wml_action/lexer.rex.rb', line 31

def state
  @state
end

Instance Method Details

#_reduce_none(val, _values, result) ⇒ Object



311
312
313
# File 'lib/wml_action/parser.tab.rb', line 311

def _reduce_none(val, _values, result)
  val[0]
end

#actionObject



41
42
43
# File 'lib/wml_action/lexer.rex.rb', line 41

def action
  yield
end

#matchesObject



35
36
37
38
39
# File 'lib/wml_action/lexer.rex.rb', line 35

def matches
  m = (1..9).map { |i| ss[i] }
  m.pop until m[-1] or m.empty?
  m
end

#next_tokenObject



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/wml_action/lexer.rex.rb', line 64

def next_token

  token = nil

  until ss.eos? or token do
    token =
      case state
      when nil then
        case
        when text = ss.scan(/#{COMMENT}/) then
          # do nothing
        when text = ss.scan(/#{OTAG}/) then
          action { [:OTAG, match[1]] }
        when text = ss.scan(/#{CTAG}/) then
          action { [:CTAG, match[1]] }
        when text = ss.scan(/#{ATTR}/) then
          action { @state = :INATTR; [:ATTR, match[1]] }
        when text = ss.scan(/#{MACRO}/) then
          action { [:MACRO, text] }
        when text = ss.scan(/#{SLASH}/) then
          action { [:SLASH, text] }
        when text = ss.scan(/\-/) then
          action { [text,text] }
        when text = ss.scan(/\+/) then
          action { [text,text] }
        when text = ss.scan(/./) then
          # do nothing
        when text = ss.scan(/\n/) then
          # do nothing
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}): '#{text}'"
        end
      when :INATTR then
        case
        when text = ss.scan(/\n/) then
          action { @state = nil }
        when text = ss.scan(/#{BLANK}/) then
          # do nothing
        when text = ss.scan(/#{ANUMBER}\s+/) then
          action { @state = nil; [:ANUMBER, text.to_i] }
        when text = ss.scan(/#{ASTR}/) then
          action { [:ASTR, text] }
        when text = ss.scan(/#{MACRO}/) then
          action { [:AMACRO, text] }
        when text = ss.scan(/_/) then
          action { [:UNDERSC, text] }
        when text = ss.scan(/\+/) then
          action { [:APLUS, text] }
        when text = ss.scan(/#{APLAIN}/) then
          action { [:APLAIN, text] }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}): '#{text}'"
        end
      else
        raise ScanError, "undefined state: '#{state}'"
      end # token = case state

    next unless token # allow functions to trigger redo w/ nil
  end # while

  raise "bad lexical result: #{token.inspect}" unless
    token.nil? || (Array === token && token.size >= 2)

  # auto-switch state
  self.state = token.last if token && token.first == :state

  token
end

#parse(str) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/wml_action/lexer.rex.rb', line 49

def parse str
  self.ss     = scanner_class.new str
  self.lineno = 1
  self.state  ||= nil

  do_parse
end

#parse_file(path) ⇒ Object



57
58
59
60
61
62
# File 'lib/wml_action/lexer.rex.rb', line 57

def parse_file path
  self.filename = path
  open path do |f|
    parse f.read
  end
end

#scanner_classObject



45
46
47
# File 'lib/wml_action/lexer.rex.rb', line 45

def scanner_class
  StringScanner
end