rparsec 0.4.2 → 1.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.
- data/rparsec/context.rb +3 -1
- data/rparsec/error.rb +6 -1
- data/rparsec/expressions.rb +5 -1
- data/rparsec/functors.rb +4 -0
- data/rparsec/id_monad.rb +4 -0
- data/rparsec/keywords.rb +14 -2
- data/rparsec/locator.rb +4 -0
- data/rparsec/misc.rb +21 -0
- data/rparsec/monad.rb +5 -1
- data/rparsec/operators.rb +4 -18
- data/rparsec/parser.rb +3 -1
- data/rparsec/parser_monad.rb +5 -2
- data/rparsec/parsers.rb +31 -23
- data/rparsec/token.rb +22 -3
- data/test/src/functor_test.rb +1 -1
- data/test/src/keyword_test.rb +1 -0
- data/test/src/operator_test.rb +1 -0
- data/test/src/parser_test.rb +2 -2
- data/test/src/simple_monad_test.rb +1 -0
- data/test/src/sql.rb +2 -0
- data/test/src/sql_parser.rb +3 -0
- metadata +2 -2
data/rparsec/context.rb
CHANGED
data/rparsec/error.rb
CHANGED
data/rparsec/expressions.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'rparsec/parser'
|
2
2
|
|
3
|
+
module RParsec
|
4
|
+
|
3
5
|
Associativities = [:prefix, :postfix, :infixn, :infixr, :infixl]
|
4
6
|
#
|
5
7
|
# This class holds information about operator precedences
|
@@ -177,4 +179,6 @@ module Expressions
|
|
177
179
|
end
|
178
180
|
suites
|
179
181
|
end
|
180
|
-
end
|
182
|
+
end
|
183
|
+
|
184
|
+
end # module
|
data/rparsec/functors.rb
CHANGED
data/rparsec/id_monad.rb
CHANGED
data/rparsec/keywords.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'rparsec/parser'
|
2
2
|
|
3
|
+
module RParsec
|
4
|
+
|
3
5
|
#
|
4
6
|
# This class helps building lexers and parsers for keywords.
|
5
7
|
#
|
@@ -8,7 +10,15 @@ class Keywords
|
|
8
10
|
|
9
11
|
private_class_method :new
|
10
12
|
|
11
|
-
|
13
|
+
#
|
14
|
+
# The symbol used to identify a keyword token
|
15
|
+
#
|
16
|
+
attr_reader :keyword_symbol
|
17
|
+
|
18
|
+
#
|
19
|
+
# The lexer that parses all the keywords represented
|
20
|
+
#
|
21
|
+
attr_reader :lexer
|
12
22
|
|
13
23
|
#
|
14
24
|
# Do we lex case sensitively?
|
@@ -99,4 +109,6 @@ class Keywords
|
|
99
109
|
case when case_sensitive then w.dup else w.downcase end
|
100
110
|
end
|
101
111
|
end
|
102
|
-
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end # module
|
data/rparsec/locator.rb
CHANGED
data/rparsec/misc.rb
CHANGED
@@ -1,3 +1,23 @@
|
|
1
|
+
module RParsec
|
2
|
+
|
3
|
+
#
|
4
|
+
# Internal utility functions for string manipulations.
|
5
|
+
#
|
6
|
+
module StringUtils
|
7
|
+
#
|
8
|
+
# Does _str_ starts with the _sub_ string?
|
9
|
+
#
|
10
|
+
def self.starts_with? str, sub
|
11
|
+
return true if sub.nil?
|
12
|
+
len = sub.length
|
13
|
+
return false if len > str.length
|
14
|
+
for i in (0...len)
|
15
|
+
return false if str[i] != sub[i]
|
16
|
+
end
|
17
|
+
true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
1
21
|
#
|
2
22
|
# Helpers for defining ctor.
|
3
23
|
#
|
@@ -107,3 +127,4 @@ module Signature
|
|
107
127
|
end
|
108
128
|
end
|
109
129
|
|
130
|
+
end # module
|
data/rparsec/monad.rb
CHANGED
data/rparsec/operators.rb
CHANGED
@@ -1,22 +1,6 @@
|
|
1
1
|
require 'rparsec/parser'
|
2
2
|
|
3
|
-
|
4
|
-
# utility functions for string manipulations.
|
5
|
-
#
|
6
|
-
module StringUtils
|
7
|
-
#
|
8
|
-
# Does _str_ starts with the _sub_ string?
|
9
|
-
#
|
10
|
-
def self.starts_with? str, sub
|
11
|
-
return true if sub.nil?
|
12
|
-
len = sub.length
|
13
|
-
return false if len > str.length
|
14
|
-
for i in (0...len)
|
15
|
-
return false if str[i] != sub[i]
|
16
|
-
end
|
17
|
-
true
|
18
|
-
end
|
19
|
-
end
|
3
|
+
module RParsec
|
20
4
|
|
21
5
|
#
|
22
6
|
# This class helps building lexer and parser for operators.
|
@@ -114,4 +98,6 @@ class Operators
|
|
114
98
|
def self.to_array suites
|
115
99
|
suites.reverse!.flatten!
|
116
100
|
end
|
117
|
-
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end # module
|
data/rparsec/parser.rb
CHANGED
@@ -3,7 +3,8 @@ monad misc error context locator token functors parser_monad
|
|
3
3
|
}.each {|lib| require "rparsec/#{lib}"}
|
4
4
|
require 'strscan'
|
5
5
|
|
6
|
-
|
6
|
+
module RParsec
|
7
|
+
|
7
8
|
#
|
8
9
|
# Represents a parser that parses a certain grammar rule.
|
9
10
|
#
|
@@ -890,3 +891,4 @@ module Parsers
|
|
890
891
|
extend self
|
891
892
|
end
|
892
893
|
|
894
|
+
end # module
|
data/rparsec/parser_monad.rb
CHANGED
data/rparsec/parsers.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'rparsec/parser'
|
2
2
|
|
3
|
+
module RParsec
|
4
|
+
|
3
5
|
class FailureParser < Parser
|
4
6
|
init :msg
|
5
7
|
def _parse ctxt
|
@@ -21,28 +23,32 @@ class LazyParser < Parser
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
26
|
+
class Failures
|
27
|
+
def self.add_error(err, e)
|
28
|
+
return e if err.nil?
|
29
|
+
return err if e.nil?
|
30
|
+
cmp = compare_error(err, e)
|
31
|
+
return err if cmp > 0
|
32
|
+
return e if cmp < 0
|
33
|
+
err
|
34
|
+
# merge_error(err, e)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def self.get_first_element(err)
|
40
|
+
while err.kind_of?(Array)
|
41
|
+
err = err[0]
|
42
|
+
end
|
43
|
+
err
|
37
44
|
end
|
38
|
-
err
|
39
|
-
end
|
40
45
|
|
41
|
-
def compare_error(e1, e2)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
+
def self.compare_error(e1, e2)
|
47
|
+
e1, e2 = get_first_element(e1), get_first_element(e2)
|
48
|
+
return -1 if e1.index < e2.index
|
49
|
+
return 1 if e1.index > e2.index
|
50
|
+
0
|
51
|
+
end
|
46
52
|
end
|
47
53
|
|
48
54
|
###############################################
|
@@ -163,7 +169,7 @@ class PlusParser < LookAheadSensitiveParser
|
|
163
169
|
ctxt.index, ctxt.result = ind, result
|
164
170
|
return true if p._parse(ctxt)
|
165
171
|
return false unless visible(ctxt, ind)
|
166
|
-
err = add_error(err, ctxt.error)
|
172
|
+
err = Failures.add_error(err, ctxt.error)
|
167
173
|
end
|
168
174
|
ctxt.error = err
|
169
175
|
return false
|
@@ -224,7 +230,7 @@ class BestParser < Parser
|
|
224
230
|
if ctxt.error.index > err_pos
|
225
231
|
err_ind, err_pos = ctxt.index, ctxt.error.index
|
226
232
|
end
|
227
|
-
err = add_error(err, ctxt.error)
|
233
|
+
err = Failures.add_error(err, ctxt.error)
|
228
234
|
end
|
229
235
|
end
|
230
236
|
if best_ind >= 0
|
@@ -612,4 +618,6 @@ class SetIndexParser < Parser
|
|
612
618
|
end
|
613
619
|
end
|
614
620
|
|
615
|
-
Nil = ValueParser.new(nil)
|
621
|
+
Nil = ValueParser.new(nil)
|
622
|
+
|
623
|
+
end # module
|
data/rparsec/token.rb
CHANGED
@@ -1,12 +1,29 @@
|
|
1
1
|
require 'rparsec/misc'
|
2
2
|
|
3
|
+
module RParsec
|
4
|
+
|
3
5
|
#
|
4
|
-
#
|
6
|
+
# Represents a token during lexical analysis.
|
5
7
|
#
|
6
8
|
class Token
|
7
9
|
extend DefHelper
|
8
10
|
|
9
|
-
|
11
|
+
def_ctor :kind, :text, :index
|
12
|
+
|
13
|
+
#
|
14
|
+
# The type of the token
|
15
|
+
#
|
16
|
+
attr_reader :kind
|
17
|
+
|
18
|
+
#
|
19
|
+
# The text of the matched range
|
20
|
+
#
|
21
|
+
attr_reader :text
|
22
|
+
|
23
|
+
#
|
24
|
+
# The starting index of the matched range
|
25
|
+
#
|
26
|
+
attr_reader :index
|
10
27
|
|
11
28
|
#
|
12
29
|
# The length of the token.
|
@@ -21,4 +38,6 @@ class Token
|
|
21
38
|
def to_s
|
22
39
|
"#{@kind}: #{@text}"
|
23
40
|
end
|
24
|
-
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end # module
|
data/test/src/functor_test.rb
CHANGED
data/test/src/keyword_test.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'import'
|
2
2
|
import :parsers, :keywords
|
3
3
|
require 'parser_test'
|
4
|
+
|
4
5
|
class KeywordTestCase < ParserTestCase
|
5
6
|
Insensitive = Keywords.case_insensitive(%w{select from where group by order having}){|x|x.downcase}
|
6
7
|
Sensitive = Keywords.case_sensitive(%w{new delete if else then void int}){|x|x}
|
data/test/src/operator_test.rb
CHANGED
data/test/src/parser_test.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'import'
|
2
2
|
require 'rubyunit'
|
3
3
|
import :parsers, :functors
|
4
|
-
|
4
|
+
|
5
|
+
include RParsec
|
5
6
|
|
6
7
|
class ParserTestCase < RUNIT::TestCase
|
7
|
-
# extend Forwardable
|
8
8
|
include Functors
|
9
9
|
include Parsers
|
10
10
|
def assertParser(code, expected, parser)
|
data/test/src/sql.rb
CHANGED
data/test/src/sql_parser.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: rparsec
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0
|
7
|
-
date: 2008-
|
6
|
+
version: "1.0"
|
7
|
+
date: 2008-06-17 00:00:00 -05:00
|
8
8
|
summary: A Ruby Parser Combinator Framework
|
9
9
|
require_paths:
|
10
10
|
- .
|