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.
@@ -1,4 +1,4 @@
1
-
1
+ module RParsec
2
2
 
3
3
  class ParseContext
4
4
  attr_reader :error, :src, :index, :result
@@ -79,3 +79,5 @@ class ParseContext
79
79
  current
80
80
  end
81
81
  end
82
+
83
+ end # module
@@ -1,4 +1,7 @@
1
1
  require 'rparsec/misc'
2
+
3
+ module RParsec
4
+
2
5
  class ParserException < StandardError
3
6
  extend DefHelper
4
7
  def_readable :index
@@ -20,4 +23,6 @@ end
20
23
 
21
24
  class Expected < Failure
22
25
  Precedence = 100
23
- end
26
+ end
27
+
28
+ end # module
@@ -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
@@ -1,3 +1,5 @@
1
+ module RParsec
2
+
1
3
  #
2
4
  # This module provides frequently used functors.
3
5
  #
@@ -268,3 +270,5 @@ module FunctorMixin
268
270
  alias ** power
269
271
  alias * repeat
270
272
  end
273
+
274
+ end # module
@@ -1,3 +1,5 @@
1
+ module RParsec
2
+
1
3
  class IdMonad
2
4
  def value v
3
5
  v
@@ -11,3 +13,5 @@ class IdMonad
11
13
  a
12
14
  end
13
15
  end
16
+
17
+ end # module
@@ -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
- attr_reader :keyword_symbol, :lexer
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
@@ -1,5 +1,7 @@
1
1
  require 'rparsec/misc'
2
2
 
3
+ module RParsec
4
+
3
5
  class CodeLocator
4
6
  extend DefHelper
5
7
 
@@ -34,3 +36,5 @@ class CodeLocator
34
36
  return line, col
35
37
  end
36
38
  end
39
+
40
+ end # module
@@ -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
@@ -1,3 +1,5 @@
1
+ module RParsec
2
+
1
3
  #
2
4
  # module for Monad
3
5
  #
@@ -55,4 +57,6 @@ module Monad
55
57
  def plus other
56
58
  @monad.mplus(@this, other.this)
57
59
  end
58
- end
60
+ end
61
+
62
+ end # module
@@ -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
@@ -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
@@ -1,4 +1,5 @@
1
- # require 'rparsec/parser_impls'
1
+ module RParsec
2
+
2
3
  class ParserMonad
3
4
  def fail msg
4
5
  FailureParser.new(msg)
@@ -17,4 +18,6 @@ class ParserMonad
17
18
  def mplus(p1, p2)
18
19
  PlusParser.new([p1,p2]);
19
20
  end
20
- end
21
+ end
22
+
23
+ end # module
@@ -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
- def add_error(err, e)
25
- return e if err.nil?
26
- return err if e.nil?
27
- cmp = compare_error(err, e)
28
- return err if cmp > 0
29
- return e if cmp < 0
30
- err
31
- # merge_error(err, e)
32
- end
33
-
34
- def get_first_element(err)
35
- while err.kind_of?(Array)
36
- err = err[0]
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
- e1, e2 = get_first_element(e1), get_first_element(e2)
43
- return -1 if e1.index < e2.index
44
- return 1 if e1.index > e2.index
45
- 0
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
@@ -1,12 +1,29 @@
1
1
  require 'rparsec/misc'
2
2
 
3
+ module RParsec
4
+
3
5
  #
4
- # This class represents a token during lexical analysis.
6
+ # Represents a token during lexical analysis.
5
7
  #
6
8
  class Token
7
9
  extend DefHelper
8
10
 
9
- def_readable :kind, :text, :index
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
@@ -2,7 +2,7 @@ require 'import'
2
2
  require 'runit/testcase'
3
3
  import :parsers, :functors
4
4
 
5
-
5
+ include RParsec
6
6
  class Proc
7
7
  include FunctorMixin
8
8
  end
@@ -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}
@@ -1,6 +1,7 @@
1
1
  require 'import'
2
2
  import :parsers, :operators, :functors
3
3
  require 'parser_test'
4
+
4
5
  class OperatorTestCase < ParserTestCase
5
6
  Ops = Operators.new(%w{++ + - -- * / ~}, &Id)
6
7
  def verifyToken(src, op)
@@ -1,10 +1,10 @@
1
1
  require 'import'
2
2
  require 'rubyunit'
3
3
  import :parsers, :functors
4
- # require 'forwardable'
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)
@@ -2,6 +2,7 @@ require 'import'
2
2
  require 'runit/testcase'
3
3
  import :id_monad, :monad
4
4
 
5
+ include RParsec
5
6
  class Idm
6
7
  include Monad
7
8
  MyMonad = IdMonad.new
@@ -1,5 +1,7 @@
1
1
  require 'import'
2
2
  import :misc
3
+
4
+ include RParsec
3
5
  class Module
4
6
  include DefHelper
5
7
  end
@@ -1,5 +1,8 @@
1
1
  require 'import'
2
2
  import :parsers, :keywords, :operators, :functors, :expressions
3
+
4
+ include RParsec
5
+
3
6
  class Method
4
7
  include FunctorMixin
5
8
  end
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.4.2
7
- date: 2008-04-09 00:00:00 -05:00
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
  - .