meta_parse 0.1.0 → 0.2.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/example/parse_number.rb +65 -0
- data/lib/meta_parse.rb +11 -6
- data/lib/util.rb +5 -7
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f7afa74c2abca00e35ae85c8bcab25521c8fa85
|
4
|
+
data.tar.gz: f079f2d8b023f4854f70f3d45d3bbf01d51fa6a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b10ba04becf92908e3b22846f1cdf12b4a0a087e9864b2b49ef7d1c6a147b98fe57e855c3c2d13f3520a9f474b6d581f4c5f95db8e0f3a842fa87ad393ca221c
|
7
|
+
data.tar.gz: 6129682d1c27362c65aa9728583632a70826e95b4cb6d053a820502cf993c37ff061928cf9bf12f36d8620f5bc08f916cf71828e1924985ad81fd5c195d834a6
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require_relative "../lib/meta_parse"
|
2
|
+
|
3
|
+
module MetaParseExamples
|
4
|
+
class ParseNumber
|
5
|
+
include MetaParse
|
6
|
+
|
7
|
+
##
|
8
|
+
# Using Regexps and built-in parsers will always be faster. (Optimized by implementation.)
|
9
|
+
# See :pos_integer2 for reference.
|
10
|
+
|
11
|
+
match_method :integer do
|
12
|
+
seq(/-?[0-9]+/) { |(digits)| digits.to_i }
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Match any number with regexp, then select appropriate built-in parser to produce number.
|
17
|
+
|
18
|
+
match_method :number do
|
19
|
+
seq(/-?[0-9]+(\.[0-9]+)?([eE]-?[0-9])?/) { |(v)| (v =~ /\./) ? v.to_f : v.to_i }
|
20
|
+
end
|
21
|
+
|
22
|
+
match_method :sign do
|
23
|
+
seq [:'?', '-'] { |(m)| (m.count > 0) ? -1 : 1 }
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
##
|
28
|
+
# An example of how to parse an integer using a repetition reducer.
|
29
|
+
# For the sake of simpicity, only accept positive integers.
|
30
|
+
|
31
|
+
match_method :pos_integer2 do
|
32
|
+
rep /[0-9]/, nil, nil, lambda { |a, d| (a * 10 ) + (d.ord - '0'.ord) }, 0
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Exmample of how to specify source and compile explicitly.
|
37
|
+
|
38
|
+
match_method :pos_integer3 do
|
39
|
+
self.comp [:*, /[0-9]/, nil, nil, lambda { |a, d| (a * 10 ) + (d.ord - '0'.ord) }, 0]
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
##
|
44
|
+
# Convert an integer into decimal (assuming leading decimal point).
|
45
|
+
|
46
|
+
def self.decimalize(integer)
|
47
|
+
(integer.to_f) / 10**(Math::log10(integer).floor + 1)
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# Match and return a decimal number starting with the decimal point.
|
52
|
+
|
53
|
+
match_method :decimal do
|
54
|
+
seq( '.', :integer) { |dot, integer| decimalize(integer) }
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Match and return a positive or negative number, with or without a decimal part.
|
59
|
+
|
60
|
+
match_method :number2 do
|
61
|
+
seq( :sign, :pos_integer2, [:'?', :decimal] ) { |(sign, integer, (decimal))|
|
62
|
+
sign * (integer + (decimal || 0)) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/meta_parse.rb
CHANGED
@@ -16,7 +16,7 @@ module MetaParse
|
|
16
16
|
##
|
17
17
|
# Parse string using an instance method previously defined by MetaParse::ClassMethodss::match_method.
|
18
18
|
|
19
|
-
def parse_with_method(method_name, string)
|
19
|
+
def parse_with_method(method_name, string)
|
20
20
|
self.send(method_name, string.meta(self), self)
|
21
21
|
end
|
22
22
|
|
@@ -79,7 +79,9 @@ module MetaParse
|
|
79
79
|
# Used by ::match_method, which should be used instead.
|
80
80
|
|
81
81
|
def define_matcher_method(name, matcher)
|
82
|
-
self.send(:define_method, name) do |
|
82
|
+
self.send(:define_method, name) do |scanner_spec, context=nil|
|
83
|
+
scanner = scanner_spec.meta
|
84
|
+
scanner.parser ||= self
|
83
85
|
matcher.match scanner, context
|
84
86
|
end
|
85
87
|
end
|
@@ -96,6 +98,12 @@ module MetaParse
|
|
96
98
|
@parser = parser
|
97
99
|
end
|
98
100
|
|
101
|
+
##
|
102
|
+
# MetaScanner is already a MetaScanner
|
103
|
+
def meta
|
104
|
+
self
|
105
|
+
end
|
106
|
+
|
99
107
|
##
|
100
108
|
# Match and return a single character or return nil, updating internal position on match.
|
101
109
|
|
@@ -238,8 +246,7 @@ module MetaParse
|
|
238
246
|
|
239
247
|
def m(string)
|
240
248
|
match MetaScanner.new(string)
|
241
|
-
end
|
242
|
-
|
249
|
+
end
|
243
250
|
end
|
244
251
|
|
245
252
|
##
|
@@ -450,8 +457,6 @@ class String
|
|
450
457
|
|
451
458
|
def equal_at(position, string, position2=0)
|
452
459
|
for i in position2..(position2 + string.length - 1) do
|
453
|
-
puts "#{position}-#{i}"
|
454
|
-
puts "#{self[position]}-#{string[i]}"
|
455
460
|
return nil unless self[position] == string[i]
|
456
461
|
position += 1
|
457
462
|
end
|
data/lib/util.rb
CHANGED
@@ -16,17 +16,14 @@ module Util
|
|
16
16
|
# The latter allows for arbitrary substitution based on matched content.
|
17
17
|
|
18
18
|
def self.mgsub(string, substitution_hash)
|
19
|
-
all_regexps = []
|
20
19
|
regexp_hash = {}
|
21
20
|
|
22
21
|
substitution_hash.each do |key, substitution|
|
23
22
|
regexp = to_regexp(key)
|
24
|
-
all_regexps << regexp
|
25
23
|
regexp_hash[regexp] = substitution
|
26
24
|
end
|
27
25
|
|
28
|
-
|
29
|
-
combined_regexp = Regexp.union(*all_regexps)
|
26
|
+
combined_regexp = Regexp.union(*regexp_hash.keys)
|
30
27
|
|
31
28
|
substitution_function = make_substitution_function(regexp_hash)
|
32
29
|
|
@@ -68,13 +65,14 @@ module Util
|
|
68
65
|
result
|
69
66
|
}
|
70
67
|
end
|
71
|
-
end
|
72
|
-
|
68
|
+
end
|
73
69
|
|
74
|
-
|
70
|
+
module MGSub
|
71
|
+
refine String do
|
75
72
|
def mgsub(substitution_hash)
|
76
73
|
Util::mgsub(self, substitution_hash)
|
77
74
|
end
|
75
|
+
end
|
78
76
|
end
|
79
77
|
|
80
78
|
#end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: meta_parse
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chhi'mèd Künzang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
description: Functional recursive descent parsing.
|
@@ -30,6 +30,7 @@ executables: []
|
|
30
30
|
extensions: []
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
|
+
- example/parse_number.rb
|
33
34
|
- lib/meta_parse.rb
|
34
35
|
- lib/util.rb
|
35
36
|
homepage: https://github.com/clkunzang/meta_parse
|
@@ -42,17 +43,17 @@ require_paths:
|
|
42
43
|
- lib
|
43
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|
44
45
|
requirements:
|
45
|
-
- -
|
46
|
+
- - ">="
|
46
47
|
- !ruby/object:Gem::Version
|
47
48
|
version: '0'
|
48
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
50
|
requirements:
|
50
|
-
- -
|
51
|
+
- - ">="
|
51
52
|
- !ruby/object:Gem::Version
|
52
53
|
version: '0'
|
53
54
|
requirements: []
|
54
55
|
rubyforge_project:
|
55
|
-
rubygems_version: 2.
|
56
|
+
rubygems_version: 2.5.2
|
56
57
|
signing_key:
|
57
58
|
specification_version: 4
|
58
59
|
summary: Parse by recursive descent.
|