parslet 1.1.0 → 1.1.1
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/Gemfile +1 -0
- data/HISTORY.txt +6 -0
- data/example/comments.rb +37 -0
- data/example/minilisp.rb +0 -1
- data/lib/parslet/atoms/base.rb +3 -2
- data/lib/parslet/source.rb +13 -5
- metadata +4 -3
data/Gemfile
CHANGED
data/HISTORY.txt
CHANGED
data/example/comments.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# A small example on how to parse common types of comments. The example
|
2
|
+
# started out with parser code from Stephen Waits.
|
3
|
+
|
4
|
+
$:.unshift '../lib'
|
5
|
+
|
6
|
+
require 'pp'
|
7
|
+
require 'parslet'
|
8
|
+
require 'parslet/convenience'
|
9
|
+
|
10
|
+
class ALanguage < Parslet::Parser
|
11
|
+
root(:expressions)
|
12
|
+
|
13
|
+
rule(:expressions) { (line >> eol).repeat(1) | line }
|
14
|
+
rule(:line) { space? >> an_expression.as(:exp).repeat }
|
15
|
+
rule(:an_expression) { str('a').as(:a) >> space? }
|
16
|
+
|
17
|
+
rule(:eol) { space? >> match["\n\r"].repeat(1) >> space? }
|
18
|
+
|
19
|
+
rule(:space?) { space.repeat }
|
20
|
+
rule(:space) { multiline_comment.as(:multi) | line_comment.as(:line) | str(' ') }
|
21
|
+
|
22
|
+
rule(:line_comment) { str('//') >> (match["\n\r"].absnt? >> any).repeat }
|
23
|
+
rule(:multiline_comment) { str('/*') >> (str('*/').absnt? >> any).repeat >> str('*/') }
|
24
|
+
end
|
25
|
+
|
26
|
+
code = %q(
|
27
|
+
a
|
28
|
+
// line comment
|
29
|
+
a a a // line comment
|
30
|
+
a /* inline comment */ a
|
31
|
+
/* multiline
|
32
|
+
comment */
|
33
|
+
)
|
34
|
+
|
35
|
+
pp ALanguage.new.parse_with_debug(code)
|
36
|
+
|
37
|
+
|
data/example/minilisp.rb
CHANGED
data/lib/parslet/atoms/base.rb
CHANGED
@@ -33,7 +33,7 @@ class Parslet::Atoms::Base
|
|
33
33
|
# Stack trace will be off, but the error tree should explain the reason
|
34
34
|
# it failed.
|
35
35
|
if value.error?
|
36
|
-
|
36
|
+
parse_failed(value.message)
|
37
37
|
end
|
38
38
|
|
39
39
|
# assert: value is a success answer
|
@@ -85,7 +85,8 @@ class Parslet::Atoms::Base
|
|
85
85
|
# behaviour.
|
86
86
|
#
|
87
87
|
def try(source, context)
|
88
|
-
raise NotImplementedError,
|
88
|
+
raise NotImplementedError, \
|
89
|
+
"Atoms::Base doesn't have behaviour, please implement #try(source, context)."
|
89
90
|
end
|
90
91
|
|
91
92
|
# Construct a new atom that repeats the current atom min times at least and
|
data/lib/parslet/source.rb
CHANGED
@@ -21,7 +21,6 @@ class Parslet::Source
|
|
21
21
|
# but probably make a scan to that position neccessary.
|
22
22
|
@line_ends = []
|
23
23
|
@line_ends.extend RangeSearch
|
24
|
-
@eof_reached_once = false
|
25
24
|
end
|
26
25
|
|
27
26
|
def read(n)
|
@@ -100,13 +99,22 @@ class Parslet::Source
|
|
100
99
|
private
|
101
100
|
|
102
101
|
def scan_for_line_endings(start_pos, buf)
|
102
|
+
return unless buf
|
103
|
+
return unless buf.index("\n")
|
104
|
+
cur = -1
|
105
|
+
|
106
|
+
# If we have already read part or all of buf, we already know about
|
107
|
+
# line ends in that portion. remove it and correct cur (search index)
|
108
|
+
if @last_line_end && start_pos < @last_line_end
|
109
|
+
# Let's not search the range from start_pos to last_line_end again.
|
110
|
+
cur = @last_line_end - start_pos -1
|
111
|
+
end
|
112
|
+
|
103
113
|
# Scan the string for line endings; store the positions of all endings
|
104
114
|
# in @line_ends.
|
105
|
-
cur = -1
|
106
115
|
while buf && cur = buf.index("\n", cur+1)
|
107
|
-
@
|
116
|
+
@last_line_end = (start_pos + cur+1)
|
117
|
+
@line_ends << @last_line_end
|
108
118
|
end
|
109
|
-
|
110
|
-
@eof_reached_once = true
|
111
119
|
end
|
112
120
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 1.1.
|
8
|
+
- 1
|
9
|
+
version: 1.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kaspar Schiess
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-02-
|
17
|
+
date: 2011-02-04 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- lib/parslet/source.rb
|
110
110
|
- lib/parslet/transform.rb
|
111
111
|
- lib/parslet.rb
|
112
|
+
- example/comments.rb
|
112
113
|
- example/documentation.rb
|
113
114
|
- example/email_parser.rb
|
114
115
|
- example/empty.rb
|