Class: Rack::Mount::StrexpParser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/rack/mount/strexp/parser.rb,
lib/rack/mount/strexp/tokenizer.rb

Defined Under Namespace

Classes: ScanError

Constant Summary collapse

REGEXP_NAMED_CAPTURE =
'(?:<%s>%s)'.freeze
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",
"PARAM",
"GLOB",
"LPAREN",
"RPAREN",
"CHAR",
"$start",
"target",
"expr",
"token" ]
Racc_debug_parser =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



23
24
25
# File 'lib/rack/mount/strexp/parser.rb', line 23

def anchor
  @anchor
end

#filenameObject (readonly)

Returns the value of attribute filename.



14
15
16
# File 'lib/rack/mount/strexp/tokenizer.rb', line 14

def filename
  @filename
end

#linenoObject (readonly)

Returns the value of attribute lineno.



13
14
15
# File 'lib/rack/mount/strexp/tokenizer.rb', line 13

def lineno
  @lineno
end

#requirementsObject

Returns the value of attribute requirements.



23
24
25
# File 'lib/rack/mount/strexp/parser.rb', line 23

def requirements
  @requirements
end

#stateObject

Returns the value of attribute state.



15
16
17
# File 'lib/rack/mount/strexp/tokenizer.rb', line 15

def state
  @state
end

Instance Method Details

#_reduce_1(val, _values, result) ⇒ Object

reduce 0 omitted



116
117
118
119
# File 'lib/rack/mount/strexp/parser.rb', line 116

def _reduce_1(val, _values, result)
 result = anchor ? "\\A#{val.join}\\Z" : "\\A#{val.join}" 
    result
end

#_reduce_2(val, _values, result) ⇒ Object



121
122
123
124
# File 'lib/rack/mount/strexp/parser.rb', line 121

def _reduce_2(val, _values, result)
 result = val.join 
    result
end

#_reduce_4(val, _values, result) ⇒ Object

reduce 3 omitted



128
129
130
131
132
133
134
# File 'lib/rack/mount/strexp/parser.rb', line 128

def _reduce_4(val, _values, result)
           name = val[0].to_sym
           requirement = requirements[name]
           result = REGEXP_NAMED_CAPTURE % [name, requirement]
         
    result
end

#_reduce_5(val, _values, result) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/rack/mount/strexp/parser.rb', line 136

def _reduce_5(val, _values, result)
           name = val[0].to_sym
           requirement = requirements[name]
           result = REGEXP_NAMED_CAPTURE % [name, '.+' || requirement]
         
    result
end

#_reduce_6(val, _values, result) ⇒ Object



144
145
146
147
# File 'lib/rack/mount/strexp/parser.rb', line 144

def _reduce_6(val, _values, result)
 result = "(?:#{val[1]})?" 
    result
end

#_reduce_7(val, _values, result) ⇒ Object



149
150
151
152
# File 'lib/rack/mount/strexp/parser.rb', line 149

def _reduce_7(val, _values, result)
 result = Regexp.escape(val[0]) 
    result
end

#_reduce_none(val, _values, result) ⇒ Object



154
155
156
# File 'lib/rack/mount/strexp/parser.rb', line 154

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

#actionObject



23
24
25
# File 'lib/rack/mount/strexp/tokenizer.rb', line 23

def action
  yield
end

#load_file(filename) ⇒ Object



33
34
35
36
37
38
# File 'lib/rack/mount/strexp/tokenizer.rb', line 33

def load_file( filename )
  @filename = filename
  open(filename, "r") do |f|
    scan_setup(f.read)
  end
end

#next_tokenObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack/mount/strexp/tokenizer.rb', line 46

def next_token
  return if @ss.eos?

  text = @ss.peek(1)
  @lineno  +=  1  if text == "\n"
  token = case @state
  when nil
    case
    when (text = @ss.scan(/\\(\(|\)|:|\*)/))
       action { [:CHAR,  @ss[1]] }

    when (text = @ss.scan(/\:([a-zA-Z_]\w*)/))
       action { [:PARAM, @ss[1]] }

    when (text = @ss.scan(/\*([a-zA-Z_]\w*)/))
       action { [:GLOB,  @ss[1]] }

    when (text = @ss.scan(/\(/))
       action { [:LPAREN, text]  }

    when (text = @ss.scan(/\)/))
       action { [:RPAREN, text]  }

    when (text = @ss.scan(/./))
       action { [:CHAR,   text]  }

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  else
    raise  ScanError, "undefined state: '" + state.to_s + "'"
  end  # case state
  token
end

#scan_file(filename) ⇒ Object



40
41
42
43
# File 'lib/rack/mount/strexp/tokenizer.rb', line 40

def scan_file( filename )
  load_file(filename)
  do_parse
end

#scan_setup(str) ⇒ Object



17
18
19
20
21
# File 'lib/rack/mount/strexp/tokenizer.rb', line 17

def scan_setup(str)
  @ss = StringScanner.new(str)
  @lineno =  1
  @state  = nil
end

#scan_str(str) ⇒ Object Also known as: scan



27
28
29
30
# File 'lib/rack/mount/strexp/tokenizer.rb', line 27

def scan_str(str)
  scan_setup(str)
  do_parse
end