abnf-parser 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/abnf/parser.rb +23 -25
- data/lib/abnf/parser/common.rb +63 -0
- data/lib/abnf/parser/compiler.rb +73 -33
- data/lib/abnf/parser/compiler/alternation.rb +54 -0
- data/lib/abnf/parser/compiler/concatenation.rb +55 -0
- data/lib/abnf/parser/compiler/element.rb +87 -0
- data/lib/abnf/parser/compiler/group.rb +19 -0
- data/lib/abnf/parser/compiler/option.rb +19 -0
- data/lib/abnf/parser/compiler/repetition.rb +50 -0
- data/lib/abnf/parser/compiler/rule_builder.rb +32 -0
- data/lib/abnf/parser/compiler/rule_list.rb +67 -0
- data/lib/abnf/parser/compiler/token.rb +34 -0
- data/lib/abnf/parser/compiler/tokenizer.rb +95 -0
- data/lib/abnf/parser/controls.rb +5 -3
- data/lib/abnf/parser/controls/abnf.rb +237 -0
- data/lib/abnf/parser/controls/compiler/tokens.rb +97 -0
- data/lib/abnf/parser/controls/nodes.rb +113 -0
- data/lib/abnf/parser/controls/rule_lists.rb +63 -0
- data/lib/abnf/parser/controls/rule_lists/multiples_of_three.rb +146 -0
- data/lib/abnf/parser/controls/rules.rb +40 -17
- data/lib/abnf/parser/{errors.rb → error.rb} +0 -2
- data/lib/abnf/parser/node.rb +138 -0
- data/lib/abnf/parser/rule_list.rb +50 -11
- data/lib/abnf/parser/rules/alternation.rb +38 -0
- data/lib/abnf/parser/rules/concatenation.rb +36 -0
- data/lib/abnf/parser/rules/prose_val.rb +18 -0
- data/lib/abnf/parser/rules/reference.rb +22 -0
- data/lib/abnf/parser/rules/regexp_pattern.rb +35 -0
- data/lib/abnf/parser/rules/repetition.rb +46 -0
- data/lib/abnf/parser/rules/terminal.rb +63 -0
- metadata +31 -31
- data/lib/abnf/parser/ast.rb +0 -67
- data/lib/abnf/parser/controls/ast.rb +0 -28
- data/lib/abnf/parser/controls/grammar.rb +0 -32
- data/lib/abnf/parser/controls/text_stream.rb +0 -15
- data/lib/abnf/parser/grammar.rb +0 -17
- data/lib/abnf/parser/grammar/alternative.rb +0 -27
- data/lib/abnf/parser/grammar/char_val.rb +0 -15
- data/lib/abnf/parser/grammar/concatenation.rb +0 -21
- data/lib/abnf/parser/grammar/element.rb +0 -57
- data/lib/abnf/parser/grammar/num_val.rb +0 -72
- data/lib/abnf/parser/grammar/prose_val.rb +0 -15
- data/lib/abnf/parser/grammar/repetition.rb +0 -58
- data/lib/abnf/parser/grammar/rule.rb +0 -31
- data/lib/abnf/parser/grammar/rulename.rb +0 -15
- data/lib/abnf/parser/rule.rb +0 -10
- data/lib/abnf/parser/rule/alternative.rb +0 -43
- data/lib/abnf/parser/rule/concatenation.rb +0 -29
- data/lib/abnf/parser/rule/none.rb +0 -17
- data/lib/abnf/parser/rule/optional.rb +0 -24
- data/lib/abnf/parser/rule/reference.rb +0 -19
- data/lib/abnf/parser/rule/repetition.rb +0 -41
- data/lib/abnf/parser/rule/terminal_value.rb +0 -32
- data/lib/abnf/parser/rule/value_range.rb +0 -33
- data/lib/abnf/parser/rule_list/entry.rb +0 -21
- data/lib/abnf/parser/text_stream.rb +0 -67
metadata
CHANGED
@@ -1,53 +1,52 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: abnf-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Ladd
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: abnf-parser allows you to compile ABNF
|
14
|
-
to parse text.
|
13
|
+
description: abnf-parser allows you to compile ABNF (Augmented Backus-Naur Form) into
|
14
|
+
rule lists that can be used to efficiently parse text conforming to ABNF grammar.
|
15
15
|
email: nathanladd+github@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/abnf/parser.rb
|
21
|
-
- lib/abnf/parser/
|
21
|
+
- lib/abnf/parser/common.rb
|
22
22
|
- lib/abnf/parser/compiler.rb
|
23
|
+
- lib/abnf/parser/compiler/alternation.rb
|
24
|
+
- lib/abnf/parser/compiler/concatenation.rb
|
25
|
+
- lib/abnf/parser/compiler/element.rb
|
26
|
+
- lib/abnf/parser/compiler/group.rb
|
27
|
+
- lib/abnf/parser/compiler/option.rb
|
28
|
+
- lib/abnf/parser/compiler/repetition.rb
|
29
|
+
- lib/abnf/parser/compiler/rule_builder.rb
|
30
|
+
- lib/abnf/parser/compiler/rule_list.rb
|
31
|
+
- lib/abnf/parser/compiler/token.rb
|
32
|
+
- lib/abnf/parser/compiler/tokenizer.rb
|
23
33
|
- lib/abnf/parser/controls.rb
|
24
|
-
- lib/abnf/parser/controls/
|
25
|
-
- lib/abnf/parser/controls/
|
34
|
+
- lib/abnf/parser/controls/abnf.rb
|
35
|
+
- lib/abnf/parser/controls/compiler/tokens.rb
|
36
|
+
- lib/abnf/parser/controls/nodes.rb
|
37
|
+
- lib/abnf/parser/controls/rule_lists.rb
|
38
|
+
- lib/abnf/parser/controls/rule_lists/multiples_of_three.rb
|
26
39
|
- lib/abnf/parser/controls/rules.rb
|
27
|
-
- lib/abnf/parser/
|
28
|
-
- lib/abnf/parser/
|
29
|
-
- lib/abnf/parser/grammar.rb
|
30
|
-
- lib/abnf/parser/grammar/alternative.rb
|
31
|
-
- lib/abnf/parser/grammar/char_val.rb
|
32
|
-
- lib/abnf/parser/grammar/concatenation.rb
|
33
|
-
- lib/abnf/parser/grammar/element.rb
|
34
|
-
- lib/abnf/parser/grammar/num_val.rb
|
35
|
-
- lib/abnf/parser/grammar/prose_val.rb
|
36
|
-
- lib/abnf/parser/grammar/repetition.rb
|
37
|
-
- lib/abnf/parser/grammar/rule.rb
|
38
|
-
- lib/abnf/parser/grammar/rulename.rb
|
39
|
-
- lib/abnf/parser/rule.rb
|
40
|
-
- lib/abnf/parser/rule/alternative.rb
|
41
|
-
- lib/abnf/parser/rule/concatenation.rb
|
42
|
-
- lib/abnf/parser/rule/none.rb
|
43
|
-
- lib/abnf/parser/rule/optional.rb
|
44
|
-
- lib/abnf/parser/rule/reference.rb
|
45
|
-
- lib/abnf/parser/rule/repetition.rb
|
46
|
-
- lib/abnf/parser/rule/terminal_value.rb
|
47
|
-
- lib/abnf/parser/rule/value_range.rb
|
40
|
+
- lib/abnf/parser/error.rb
|
41
|
+
- lib/abnf/parser/node.rb
|
48
42
|
- lib/abnf/parser/rule_list.rb
|
49
|
-
- lib/abnf/parser/
|
50
|
-
- lib/abnf/parser/
|
43
|
+
- lib/abnf/parser/rules/alternation.rb
|
44
|
+
- lib/abnf/parser/rules/concatenation.rb
|
45
|
+
- lib/abnf/parser/rules/prose_val.rb
|
46
|
+
- lib/abnf/parser/rules/reference.rb
|
47
|
+
- lib/abnf/parser/rules/regexp_pattern.rb
|
48
|
+
- lib/abnf/parser/rules/repetition.rb
|
49
|
+
- lib/abnf/parser/rules/terminal.rb
|
51
50
|
homepage: https://github.com/ntl/abnf-parser
|
52
51
|
licenses:
|
53
52
|
- MIT
|
@@ -71,5 +70,6 @@ rubyforge_project:
|
|
71
70
|
rubygems_version: 2.4.5.1
|
72
71
|
signing_key:
|
73
72
|
specification_version: 4
|
74
|
-
summary: ABNF
|
73
|
+
summary: ABNF (Augmented Backus-Naur Form) Parsing Library
|
75
74
|
test_files: []
|
75
|
+
has_rdoc:
|
data/lib/abnf/parser/ast.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
module ABNF
|
2
|
-
module Parser
|
3
|
-
module AST
|
4
|
-
def self.leaf *arguments
|
5
|
-
Leaf.new *arguments
|
6
|
-
end
|
7
|
-
|
8
|
-
class Leaf
|
9
|
-
attr_reader :text
|
10
|
-
|
11
|
-
def initialize text
|
12
|
-
@text = text
|
13
|
-
end
|
14
|
-
|
15
|
-
def == other_node
|
16
|
-
other_node.respond_to? :text and text == other_node.text
|
17
|
-
end
|
18
|
-
|
19
|
-
def inner_text
|
20
|
-
text
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.repetition *arguments
|
25
|
-
Repetition.new *arguments
|
26
|
-
end
|
27
|
-
|
28
|
-
class Repetition
|
29
|
-
attr_reader :nodes
|
30
|
-
|
31
|
-
def initialize *nodes
|
32
|
-
@nodes = nodes
|
33
|
-
end
|
34
|
-
|
35
|
-
def == other_node
|
36
|
-
other_node.respond_to? :nodes and nodes == other_node.nodes
|
37
|
-
end
|
38
|
-
|
39
|
-
def inner_text
|
40
|
-
strings = nodes.map &:inner_text
|
41
|
-
strings.join
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.sequence *arguments
|
46
|
-
Sequence.new *arguments
|
47
|
-
end
|
48
|
-
|
49
|
-
class Sequence
|
50
|
-
attr_reader :nodes
|
51
|
-
|
52
|
-
def initialize *nodes
|
53
|
-
@nodes = nodes
|
54
|
-
end
|
55
|
-
|
56
|
-
def == other_node
|
57
|
-
other_node.respond_to? :nodes and nodes == other_node.nodes
|
58
|
-
end
|
59
|
-
|
60
|
-
def inner_text
|
61
|
-
strings = nodes.map &:inner_text
|
62
|
-
strings.join
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module ABNF
|
2
|
-
module Parser
|
3
|
-
module Controls
|
4
|
-
module AST
|
5
|
-
extend self
|
6
|
-
|
7
|
-
def leaf text = nil
|
8
|
-
text ||= 'some-string'
|
9
|
-
Parser::AST.leaf text
|
10
|
-
end
|
11
|
-
|
12
|
-
def repetition
|
13
|
-
Parser::AST.repetition(
|
14
|
-
leaf('some-string'),
|
15
|
-
leaf('other-string'),
|
16
|
-
)
|
17
|
-
end
|
18
|
-
|
19
|
-
def sequence
|
20
|
-
Parser::AST.sequence(
|
21
|
-
leaf('some-string'),
|
22
|
-
leaf('other-string'),
|
23
|
-
)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
module ABNF
|
2
|
-
module Parser
|
3
|
-
module Controls
|
4
|
-
module Grammar
|
5
|
-
class Example
|
6
|
-
attr_reader :abnf
|
7
|
-
attr_reader :type
|
8
|
-
|
9
|
-
def initialize abnf, type
|
10
|
-
@abnf = abnf
|
11
|
-
@type = type
|
12
|
-
end
|
13
|
-
|
14
|
-
def rule
|
15
|
-
@rule ||= type.compile abnf
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
Alternative = Example.new '1*%x30-39 / *%x30-39 "*" *%x30-39', Parser::Grammar::Alternative
|
20
|
-
CharVal = Example.new '"some-string"', Parser::Grammar::CharVal
|
21
|
-
Concatenation = Example.new '"some-string" " " "some-string"', Parser::Grammar::Concatenation
|
22
|
-
Group = Example.new '( "foo" / "bar" )', Parser::Grammar::Element
|
23
|
-
NumVal = Example.new '%x41.42.43', Parser::Grammar::NumVal
|
24
|
-
Option = Example.new '[ "some-string" ]', Parser::Grammar::Element
|
25
|
-
Repetition = Example.new '1*2"some-element"', Parser::Grammar::Repetition
|
26
|
-
Rulename = Example.new 'some-rule', Parser::Grammar::Rulename
|
27
|
-
Rule = Example.new "some-rule = \"some-string\"\r\n", Parser::Grammar::Rule
|
28
|
-
ProseVal = Example.new '<Some Prose>', Parser::Grammar::ProseVal
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
data/lib/abnf/parser/grammar.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module ABNF
|
2
|
-
module Parser
|
3
|
-
module Grammar
|
4
|
-
class Alternative
|
5
|
-
include Compiler
|
6
|
-
|
7
|
-
DELIMITER = %r{/}
|
8
|
-
|
9
|
-
def call
|
10
|
-
concatenations = []
|
11
|
-
|
12
|
-
begin
|
13
|
-
concatenation = Concatenation.compile stream
|
14
|
-
break unless concatenation
|
15
|
-
concatenations << concatenation
|
16
|
-
end while match? stream
|
17
|
-
|
18
|
-
Parser::Rule::Alternative.new *concatenations
|
19
|
-
end
|
20
|
-
|
21
|
-
def match? stream
|
22
|
-
stream.match C_WSP and stream.match DELIMITER and stream.match C_WSP
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module ABNF
|
2
|
-
module Parser
|
3
|
-
module Grammar
|
4
|
-
class Concatenation
|
5
|
-
include Compiler
|
6
|
-
|
7
|
-
def call
|
8
|
-
repetitions = []
|
9
|
-
|
10
|
-
begin
|
11
|
-
repetition = Repetition.compile stream
|
12
|
-
break unless repetition
|
13
|
-
repetitions << repetition
|
14
|
-
end while stream.match C_WSP
|
15
|
-
|
16
|
-
Parser::Rule::Concatenation.new *repetitions
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
module ABNF
|
2
|
-
module Parser
|
3
|
-
module Grammar
|
4
|
-
class Element
|
5
|
-
include Compiler
|
6
|
-
|
7
|
-
GROUP_START = %r{\(}
|
8
|
-
GROUP_STOP = %r{\)}
|
9
|
-
OPTION_START = %r{\[}
|
10
|
-
OPTION_STOP = %r{\]}
|
11
|
-
|
12
|
-
def call
|
13
|
-
if stream.next_character == '['.freeze
|
14
|
-
option
|
15
|
-
elsif stream.next_character == '('.freeze
|
16
|
-
group
|
17
|
-
else
|
18
|
-
terminal
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def group
|
23
|
-
stream.match GROUP_START
|
24
|
-
stream.match C_WSP
|
25
|
-
|
26
|
-
alternative = Alternative.compile stream
|
27
|
-
|
28
|
-
stream.match C_WSP
|
29
|
-
stream.match GROUP_STOP
|
30
|
-
|
31
|
-
alternative
|
32
|
-
end
|
33
|
-
|
34
|
-
def option
|
35
|
-
stream.match OPTION_START
|
36
|
-
stream.match C_WSP
|
37
|
-
|
38
|
-
alternative = Alternative.compile stream
|
39
|
-
option = Parser::Rule::Optional.new alternative
|
40
|
-
|
41
|
-
stream.match C_WSP
|
42
|
-
stream.match OPTION_STOP
|
43
|
-
|
44
|
-
option
|
45
|
-
end
|
46
|
-
|
47
|
-
def terminal
|
48
|
-
[Rulename, CharVal, NumVal, ProseVal].each do |type|
|
49
|
-
output = type.compile stream
|
50
|
-
return output if output
|
51
|
-
end
|
52
|
-
nil
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
module ABNF
|
2
|
-
module Parser
|
3
|
-
module Grammar
|
4
|
-
class NumVal
|
5
|
-
include Compiler
|
6
|
-
|
7
|
-
PATTERN = %r{\A%
|
8
|
-
(?:
|
9
|
-
(?<base_code>x)(?<first_character>[[:xdigit:]]+)
|
10
|
-
(?:(?:-(?<range_end>[[:xdigit:]]+))|(?<sequence>(?:\.[[:xdigit:]]+)+))?
|
11
|
-
|
|
12
|
-
(?<base_code>d)(?<first_character>[[:digit:]]+)
|
13
|
-
(?:(?:-(?<range_end>[[:digit:]]+))|(?<sequence>(?:\.[[:digit:]]+)+))?
|
14
|
-
|
|
15
|
-
(?<base_code>b)(?<first_character>[01]+)
|
16
|
-
(?:(?:-(?<range_end>[01]+))|(?<sequence>(?:\.[01]+)+))?
|
17
|
-
)
|
18
|
-
}nx
|
19
|
-
|
20
|
-
def base
|
21
|
-
base_code = match_data['base_code'.freeze]
|
22
|
-
|
23
|
-
{
|
24
|
-
'b'.freeze => 2,
|
25
|
-
'd'.freeze => 10,
|
26
|
-
'x'.freeze => 16,
|
27
|
-
}[base_code]
|
28
|
-
end
|
29
|
-
|
30
|
-
def build_string
|
31
|
-
*characters = sequence.to_s.split '.'
|
32
|
-
|
33
|
-
[first_character, *characters].reduce String.new do |string, character|
|
34
|
-
octet = convert_hex_character character
|
35
|
-
string << octet
|
36
|
-
string
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def call
|
41
|
-
if range_end
|
42
|
-
first_octet = convert_hex_character first_character
|
43
|
-
last_octet = convert_hex_character range_end
|
44
|
-
return Parser::Rule::ValueRange.new first_octet, last_octet
|
45
|
-
end
|
46
|
-
|
47
|
-
Parser::Rule::TerminalValue.new build_string, true
|
48
|
-
end
|
49
|
-
|
50
|
-
def convert_hex_character hex
|
51
|
-
character = hex.to_i base
|
52
|
-
octet = character.chr
|
53
|
-
octet
|
54
|
-
end
|
55
|
-
|
56
|
-
def first_character
|
57
|
-
match_data['first_character'.freeze]
|
58
|
-
end
|
59
|
-
|
60
|
-
def range_end
|
61
|
-
match_data['range_end'.freeze]
|
62
|
-
end
|
63
|
-
|
64
|
-
def sequence
|
65
|
-
sequence = match_data['sequence'.freeze]
|
66
|
-
sequence.slice! 0, 1 if sequence
|
67
|
-
sequence
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|