Class: Pegex::Pegex::AST
Instance Attribute Summary
Attributes inherited from Receiver
Instance Method Summary collapse
- #get_group(group) ⇒ Object
- #got_all_group(got) ⇒ Object
- #got_any_group(got) ⇒ Object
- #got_bracketed_group(got) ⇒ Object
- #got_error_message(got) ⇒ Object
- #got_grammar(got) ⇒ Object
- #got_meta_section(got) ⇒ Object
- #got_regular_expression(got) ⇒ Object
- #got_rule_definition(got) ⇒ Object
- #got_rule_part(got) ⇒ Object
- #got_rule_reference(got) ⇒ Object
- #got_whitespace_token(got) ⇒ Object
-
#initialize ⇒ AST
constructor
A new instance of AST.
- #set_quantity(object, quantifier) ⇒ Object
Methods inherited from Tree
Methods inherited from Receiver
Constructor Details
#initialize ⇒ AST
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/pegex/pegex/ast.rb', line 5 def initialize @atoms = Pegex::Grammar::Atoms.new.atoms @extra_rules = {} @prefixes = { '!' => ['+asr', -1], '=' => ['+asr', 1], '.' => '-skip', '-' => '-pass', '+' => '-wrap', } end |
Instance Method Details
#get_group(group) ⇒ Object
79 80 81 |
# File 'lib/pegex/pegex/ast.rb', line 79 def get_group group return group.flatten end |
#got_all_group(got) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/pegex/pegex/ast.rb', line 65 def got_all_group got list = get_group(got) fail unless list.length > 0 return list.first if list.length == 1 return '.all' => list end |
#got_any_group(got) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/pegex/pegex/ast.rb', line 72 def got_any_group got list = get_group(got) fail unless list.length > 0 return list.first if list.length == 1 return '.any' => list end |
#got_bracketed_group(got) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pegex/pegex/ast.rb', line 54 def got_bracketed_group got prefix, group, suffix = got unless prefix.empty? group[@prefixes[prefix]] = 1 end unless suffix.empty? set_quantity(group, suffix) end return group end |
#got_error_message(got) ⇒ Object
124 125 126 |
# File 'lib/pegex/pegex/ast.rb', line 124 def got return '.err' => got end |
#got_grammar(got) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/pegex/pegex/ast.rb', line 17 def got_grammar got , rule_section = got grammar = {'+toprule' => @toprule}.merge(@extra_rules).merge() rule_section.each do |rule| key, value = rule.first grammar[key] = value end return grammar end |
#got_meta_section(got) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pegex/pegex/ast.rb', line 28 def got = {} got.each do |next_| key, val = next_ key = "+#{key}" old = [key] if ! old.nil? if old.kind_of? Array old << val else [key] = [ old, val ] end else [key] = val end end return end |
#got_regular_expression(got) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/pegex/pegex/ast.rb', line 113 def got_regular_expression got got.gsub! /\s*#.*\n/, '' got.gsub! /\s+/, '' got.gsub! /\((\:|\=|\!)/, '(?\1' return {'.rgx' => got} end |
#got_rule_definition(got) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/pegex/pegex/ast.rb', line 47 def got_rule_definition got name, value = got @toprule = name if name == 'TOP' @toprule ||= name return { name => value } end |
#got_rule_part(got) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/pegex/pegex/ast.rb', line 83 def got_rule_part got rule, sep_op, sep_rule = got if sep_rule sep_rule['+eok'] = true if sep_op == '%%' rule['.sep'] = sep_rule end return rule end |
#got_rule_reference(got) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/pegex/pegex/ast.rb', line 92 def got_rule_reference got prefix, ref1, ref2, suffix = got ref = ref1 || ref2 # TODO: determine if ref1 is falsy enough node = { '.ref' => ref } if (regex = @atoms[ref]) @extra_rules[ref] = {'.rgx' => regex} end if !suffix.empty? set_quantity(node, suffix) end if !prefix.empty? if @prefixes[prefix].kind_of? Array key, val = @prefixes[prefix] else key, val = @prefixes[prefix], 1 end node[key] = val end return node end |
#got_whitespace_token(got) ⇒ Object
120 121 122 |
# File 'lib/pegex/pegex/ast.rb', line 120 def got_whitespace_token got return '.rgx' => "<ws#{got.length}>" end |
#set_quantity(object, quantifier) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pegex/pegex/ast.rb', line 128 def set_quantity object, quantifier case quantifier when ?* object['+min'] = 0 when ?+ object['+min'] = 1 when ?? object['+max'] = 1 when /^(\d+)\+$/ object['+min'] = $1 when /^(\d+)\-(\d+)+$/ object['+min'] = $1 object['+max'] = $2 when /^(\d+)$/ object['+min'] = $1 object['+max'] = $1 else fail "Invalid quantifier: '#{quantifier}'" end end |