Class: Arugula::Parser
- Inherits:
-
Object
- Object
- Arugula::Parser
- Defined in:
- lib/arugula/parser.rb
Instance Attribute Summary collapse
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Instance Method Summary collapse
- #consume ⇒ Object
-
#initialize(str) ⇒ Parser
constructor
A new instance of Parser.
- #parse! ⇒ Object
- #pop_part ⇒ Object
- #push_part(name, *content) ⇒ Object
- #state ⇒ Object
- #wrap_state(name) ⇒ Object
Constructor Details
Instance Attribute Details
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
5 6 7 |
# File 'lib/arugula/parser.rb', line 5 def pattern @pattern end |
Instance Method Details
#consume ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/arugula/parser.rb', line 27 def consume tok = pattern.slice!(0) peek = pattern.chr if tok.nil? fail 'shouldnt happen' elsif tok == '[' push_part(:characterclass) elsif tok == '-' && characterclass_type? && state.parts.last.class.type == :literal && peek != ']' literal = state.parts.pop push_part(:range, literal.literal, peek) pattern.slice!(0) elsif tok == ']' && characterclass_type? pop_part elsif tok == '$' push_part(:eol) elsif tok == '^' push_part(:sol) elsif tok == '\\' tok = pattern.slice!(0) case tok when nil fail 'unterminated escape sequence' when *MetacharacterPart::MATCHERS.keys.map(&:to_s) push_part(:metacharacter, tok) else push_part(:literal, tok) end elsif characterclass_type? push_part(:literal, tok) elsif tok == '.' push_part(:dot) elsif tok == '*' wrap_state(:star) elsif tok == '+' wrap_state(:plus) else push_part(:literal, tok) end end |
#parse! ⇒ Object
15 16 17 18 |
# File 'lib/arugula/parser.rb', line 15 def parse! consume until pattern.empty? @states.first end |
#pop_part ⇒ Object
84 85 86 87 |
# File 'lib/arugula/parser.rb', line 84 def pop_part @states.pop until @states.last.respond_to?(:parts) @states.pop end |
#push_part(name, *content) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/arugula/parser.rb', line 70 def push_part(name, *content) part_class = Part.all.find { |p| p.type == name } fail "Unknown part type #{name}" unless part_class part = part_class.new(*content) state.parts << part @states << part unless name == :literal end |
#state ⇒ Object
11 12 13 |
# File 'lib/arugula/parser.rb', line 11 def state @states.reverse_each.find { |s| s.respond_to?(:parts) } end |