Class: ABNF
- Inherits:
-
Object
- Object
- ABNF
- Defined in:
- lib/abnftt.rb
Constant Summary collapse
- FIXUP_NAMES =
Hash.new {|h, k| k}
- @@parser =
ABNFGrammarParser.new
- @@gensym =
0
Instance Attribute Summary collapse
-
#ast ⇒ Object
Returns the value of attribute ast.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#rules ⇒ Object
Returns the value of attribute rules.
-
#tree ⇒ Object
Returns the value of attribute tree.
Class Method Summary collapse
Instance Method Summary collapse
- #generate ⇒ Object
- #generate1(what) ⇒ Object
-
#initialize(ast_) ⇒ ABNF
constructor
A new instance of ABNF.
- #to_treetop(modname) ⇒ Object
- #to_treetop0(ast) ⇒ Object
- #to_treetop1(ast) ⇒ Object
- #validate(s) ⇒ Object
Constructor Details
#initialize(ast_) ⇒ ABNF
Returns a new instance of ABNF.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/abnftt.rb', line 57 def initialize(ast_) @ast = ast_ @tree = ast.ast @rules = {} @tree.each do |x| op, name, val, rest = x fail rest if rest fail op unless op == "=" # XXX if @rules[name] fail "duplicate rule for name #{name}" end @rules[name] = val end end |
Instance Attribute Details
#ast ⇒ Object
Returns the value of attribute ast.
56 57 58 |
# File 'lib/abnftt.rb', line 56 def ast @ast end |
#parser ⇒ Object
Returns the value of attribute parser.
190 191 192 |
# File 'lib/abnftt.rb', line 190 def parser @parser end |
#rules ⇒ Object
Returns the value of attribute rules.
56 57 58 |
# File 'lib/abnftt.rb', line 56 def rules @rules end |
#tree ⇒ Object
Returns the value of attribute tree.
56 57 58 |
# File 'lib/abnftt.rb', line 56 def tree @tree end |
Class Method Details
.from_abnf(s) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/abnftt.rb', line 48 def self.from_abnf(s) ast = @@parser.parse s if !ast fail self.reason(@@parser, s) end ABNF.new(ast) end |
.reason(parser, s) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/abnftt.rb', line 37 def self.reason(parser, s) reason = [parser.failure_reason] parser.failure_reason =~ /^(Expected .+) after/m reason << "#{$1.gsub("\n", '<<<NEWLINE>>>')}:" if $1 if line = s.lines.to_a[parser.failure_line - 1] reason << line reason << "#{'~' * (parser.failure_column - 1)}^" end reason.join("\n") end |
Instance Method Details
#generate ⇒ Object
72 73 74 |
# File 'lib/abnftt.rb', line 72 def generate generate1(rules.first.first) end |
#generate1(what) ⇒ Object
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 |
# File 'lib/abnftt.rb', line 76 def generate1(what) case what when String expansion = rules[what] fail "can't find rules #{what}" unless expansion generate1(expansion) when Array op, *args = what case op when "seq" args.map {|arg| generate1(arg)}.join when "alt" generate1(args.sample) when "rep" l, h, x, rest = args fail rest if rest h = l+3 if h == true n = rand(h-l+1)+l (0...n).map { generate1(x) }.join when "ci" s, rest = args fail rest if rest s.chars.map{|x|[x.upcase, x.downcase].sample}.join when "cs" s, rest = args fail rest if rest s when "char-range" l, r = args fail rest if rest (rand(r.ord-l.ord+1)+l.ord).chr(Encoding::UTF_8) when "prose" # ["prose", text] fail "prose not implemented #{what.inspect}" when "im" warn "abnftt-style inline module ignored #{what.inspect}" '' else fail [op, args].inspect end else fail end end |
#to_treetop(modname) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/abnftt.rb', line 121 def to_treetop(modname) <<~EOS # Encoding: UTF-8 grammar #{modname} #{tree.map {|x| to_treetop0(x)}.join} end EOS end |
#to_treetop0(ast) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/abnftt.rb', line 129 def to_treetop0(ast) fail ast.inspect unless ast[0] == "=" <<~EOS rule #{to_treetop1(ast[1])} #{to_treetop1(ast[2])} end EOS end |
#to_treetop1(ast) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/abnftt.rb', line 141 def to_treetop1(ast) case ast when String FIXUP_NAMES[ast].gsub("-", "_") when Array case ast[0] when "alt" # ["alt", *a] "(#{ast[1..-1].map {|x| to_treetop1(x)}.join(" / ")})" when "seq" # ["seq", *a] "(#{ast[1..-1].map {|x| to_treetop1(x)}.join(" ")})" when "rep" # ["rep", s, e, a] t = to_treetop1(ast[3]) || "@@@" case [ast[1], ast[2]] when [0, 1] t + "?" when [0, true] t + "*" when [1, true] t + "+" else t + " #{ast[1]}..#{ast[2] == true ? '' : ast[2]}" end when "prose" # ["prose", text] fail "prose not implemented #{ast.inspect}" when "ci" # ["ci", text] s = ast[1] if s =~ /\A[^A-Za-z]*\z/ s.inspect else s.inspect << "i" # could do this always, but reduce noise end when "cs" # ["cs", text] ast[1].inspect when "char-range" # ["char-range", c1, c2] c1 = Regexp.quote(ast[1]) c2 = Regexp.quote(ast[2]) "[#{c1}-#{c2}]" # XXX does that always work when "im" # ["im", a, text] to_treetop1(ast[1]) + " " + ast[2] else fail "to_treetop(#{ast.inspect})" end else fail "to_treetop(#{ast.inspect})" end end |
#validate(s) ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/abnftt.rb', line 191 def validate(s) @parser ||= Treetop.load_from_string(to_treetop("ABNF_Mod" << (@@gensym += 1).to_s)) parser_instance ||= @parser.new unless result1 = parser_instance.parse(s) fail self.class.reason(parser_instance, s) end end |