language-ruby 0.5.6 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/.ruby-version +1 -0
- data/.tool-versions +1 -1
- data/Gemfile +5 -4
- data/Gemfile.lock +12 -23
- data/README.md +2 -102
- data/language-ruby.gemspec +7 -5
- data/lib/language/atom/absent.rb +29 -0
- data/lib/language/atom/aka.rb +32 -0
- data/lib/language/atom/and.rb +32 -0
- data/lib/language/atom/any.rb +15 -0
- data/lib/language/atom/ignore.rb +27 -0
- data/lib/language/atom/maybe.rb +32 -0
- data/lib/language/atom/or.rb +46 -0
- data/lib/language/atom/repeat.rb +57 -0
- data/lib/language/atom/str.rb +21 -0
- data/lib/language/atom/then.rb +21 -0
- data/lib/language/atom.rb +10 -291
- data/lib/language/output.rb +13 -11
- data/lib/language/parser/absent/present.rb +2 -0
- data/lib/language/parser/absent.rb +2 -0
- data/lib/language/parser/end_of_input.rb +2 -0
- data/lib/language/parser/interuption.rb +2 -0
- data/lib/language/parser/not_end_of_input.rb +2 -0
- data/lib/language/parser/str/not_found.rb +2 -0
- data/lib/language/parser/str.rb +2 -0
- data/lib/language/parser.rb +10 -10
- data/lib/language/version.rb +5 -0
- data/lib/language-ruby.rb +2 -5
- data/lib/language.rb +9 -7
- data/spec/language_spec.rb +21 -0
- data/spec/spec_helper.rb +2 -5
- metadata +18 -218
- data/.cherry.js +0 -21
- data/.editorconfig +0 -9
- data/.github/workflows/rspec.yml +0 -14
- data/.prettierrc +0 -3
- data/CHANGELOG.md +0 -55
- data/LICENSE +0 -7
- data/TODO +0 -17
- data/bin/code +0 -76
- data/bin/format +0 -3
- data/bin/publish +0 -19
- data/bin/template +0 -85
- data/bin/test +0 -17
- data/code-ruby.gemspec +0 -17
- data/docs/class.code +0 -9
- data/docs/euler/1.template +0 -10
- data/docs/euler/2.template +0 -16
- data/docs/euler/3.template +0 -16
- data/docs/euler/4.template +0 -10
- data/docs/euler/5.template +0 -13
- data/docs/fibonnaci.template +0 -14
- data/docs/meetup.code +0 -12
- data/docs/precedence.template +0 -36
- data/docs/rain.code +0 -22
- data/docs/slack.code +0 -17
- data/docs/stripe.code +0 -7
- data/docs/twitter.code +0 -9
- data/lib/code/error.rb +0 -18
- data/lib/code/node/base_10.rb +0 -29
- data/lib/code/node/base_16.rb +0 -13
- data/lib/code/node/base_2.rb +0 -13
- data/lib/code/node/base_8.rb +0 -13
- data/lib/code/node/boolean.rb +0 -22
- data/lib/code/node/call.rb +0 -47
- data/lib/code/node/call_argument.rb +0 -21
- data/lib/code/node/chained_call.rb +0 -23
- data/lib/code/node/code.rb +0 -20
- data/lib/code/node/decimal.rb +0 -26
- data/lib/code/node/dictionnary.rb +0 -33
- data/lib/code/node/equal.rb +0 -34
- data/lib/code/node/function.rb +0 -20
- data/lib/code/node/function_parameter.rb +0 -31
- data/lib/code/node/if.rb +0 -59
- data/lib/code/node/if_modifier.rb +0 -47
- data/lib/code/node/list.rb +0 -16
- data/lib/code/node/negation.rb +0 -15
- data/lib/code/node/not.rb +0 -15
- data/lib/code/node/nothing.rb +0 -12
- data/lib/code/node/number.rb +0 -25
- data/lib/code/node/operation.rb +0 -38
- data/lib/code/node/power.rb +0 -20
- data/lib/code/node/rescue.rb +0 -17
- data/lib/code/node/splat.rb +0 -15
- data/lib/code/node/statement.rb +0 -59
- data/lib/code/node/string.rb +0 -53
- data/lib/code/node/ternary.rb +0 -24
- data/lib/code/node/unary_minus.rb +0 -15
- data/lib/code/node/while.rb +0 -35
- data/lib/code/node.rb +0 -13
- data/lib/code/object/argument.rb +0 -32
- data/lib/code/object/boolean.rb +0 -27
- data/lib/code/object/decimal.rb +0 -162
- data/lib/code/object/dictionnary.rb +0 -96
- data/lib/code/object/function.rb +0 -64
- data/lib/code/object/global.rb +0 -42
- data/lib/code/object/integer.rb +0 -221
- data/lib/code/object/list.rb +0 -207
- data/lib/code/object/nothing.rb +0 -23
- data/lib/code/object/number.rb +0 -6
- data/lib/code/object/range.rb +0 -146
- data/lib/code/object/ruby_function.rb +0 -31
- data/lib/code/object/string.rb +0 -102
- data/lib/code/object.rb +0 -197
- data/lib/code/parser/addition.rb +0 -21
- data/lib/code/parser/and_operator.rb +0 -17
- data/lib/code/parser/bitwise_and.rb +0 -17
- data/lib/code/parser/bitwise_or.rb +0 -21
- data/lib/code/parser/boolean.rb +0 -17
- data/lib/code/parser/call.rb +0 -122
- data/lib/code/parser/chained_call.rb +0 -47
- data/lib/code/parser/class.rb +0 -45
- data/lib/code/parser/code.rb +0 -25
- data/lib/code/parser/dictionnary.rb +0 -67
- data/lib/code/parser/equal.rb +0 -94
- data/lib/code/parser/equality.rb +0 -35
- data/lib/code/parser/equality_lower.rb +0 -9
- data/lib/code/parser/function.rb +0 -85
- data/lib/code/parser/greater.rb +0 -25
- data/lib/code/parser/group.rb +0 -22
- data/lib/code/parser/if.rb +0 -63
- data/lib/code/parser/if_modifier.rb +0 -55
- data/lib/code/parser/list.rb +0 -42
- data/lib/code/parser/multiplication.rb +0 -25
- data/lib/code/parser/name.rb +0 -107
- data/lib/code/parser/negation.rb +0 -30
- data/lib/code/parser/not_keyword.rb +0 -23
- data/lib/code/parser/nothing.rb +0 -22
- data/lib/code/parser/number.rb +0 -154
- data/lib/code/parser/operation.rb +0 -35
- data/lib/code/parser/or_keyword.rb +0 -21
- data/lib/code/parser/or_operator.rb +0 -17
- data/lib/code/parser/power.rb +0 -43
- data/lib/code/parser/range.rb +0 -17
- data/lib/code/parser/rescue.rb +0 -39
- data/lib/code/parser/shift.rb +0 -21
- data/lib/code/parser/splat.rb +0 -31
- data/lib/code/parser/statement.rb +0 -9
- data/lib/code/parser/string.rb +0 -78
- data/lib/code/parser/ternary.rb +0 -46
- data/lib/code/parser/unary_minus.rb +0 -31
- data/lib/code/parser/while.rb +0 -36
- data/lib/code/parser/whitespace.rb +0 -49
- data/lib/code/parser.rb +0 -19
- data/lib/code/ruby.rb +0 -174
- data/lib/code-ruby.rb +0 -10
- data/lib/code.rb +0 -47
- data/lib/template/node/code_part.rb +0 -13
- data/lib/template/node/part.rb +0 -19
- data/lib/template/node/template.rb +0 -15
- data/lib/template/node/text_part.rb +0 -13
- data/lib/template/node.rb +0 -4
- data/lib/template/parser/template.rb +0 -39
- data/lib/template/parser.rb +0 -19
- data/lib/template/version.rb +0 -3
- data/lib/template-ruby.rb +0 -10
- data/lib/template.rb +0 -50
- data/spec/code/addition_spec.rb +0 -13
- data/spec/code/and_operator_spec.rb +0 -13
- data/spec/code/bitwise_and_spec.rb +0 -13
- data/spec/code/bitwise_or_spec.rb +0 -13
- data/spec/code/boolean_spec.rb +0 -13
- data/spec/code/call_spec.rb +0 -21
- data/spec/code/chained_call_spec.rb +0 -16
- data/spec/code/code_spec.rb +0 -29
- data/spec/code/dictionnary_spec.rb +0 -17
- data/spec/code/equal_spec.rb +0 -26
- data/spec/code/equality_spec.rb +0 -13
- data/spec/code/function_spec.rb +0 -18
- data/spec/code/greater_spec.rb +0 -18
- data/spec/code/group_spec.rb +0 -12
- data/spec/code/if_modifier_spec.rb +0 -20
- data/spec/code/if_spec.rb +0 -25
- data/spec/code/list_spec.rb +0 -19
- data/spec/code/multiplication_spec.rb +0 -18
- data/spec/code/negation_spec.rb +0 -20
- data/spec/code/not_keyword_spec.rb +0 -13
- data/spec/code/nothing_spec.rb +0 -17
- data/spec/code/number_spec.rb +0 -22
- data/spec/code/or_keyword_spec.rb +0 -17
- data/spec/code/or_operator_spec.rb +0 -16
- data/spec/code/parser/boolean_spec.rb +0 -16
- data/spec/code/parser/call_spec.rb +0 -26
- data/spec/code/parser/chained_call.rb +0 -17
- data/spec/code/parser/dictionnary_spec.rb +0 -18
- data/spec/code/parser/function_spec.rb +0 -16
- data/spec/code/parser/group_spec.rb +0 -18
- data/spec/code/parser/list_spec.rb +0 -18
- data/spec/code/parser/number_spec.rb +0 -12
- data/spec/code/parser/string_spec.rb +0 -21
- data/spec/code/parser_spec.rb +0 -23
- data/spec/code/power_spec.rb +0 -13
- data/spec/code/range_spec.rb +0 -16
- data/spec/code/rescue_spec.rb +0 -13
- data/spec/code/shift_spec.rb +0 -13
- data/spec/code/splat_spec.rb +0 -13
- data/spec/code/string_spec.rb +0 -27
- data/spec/code/ternary_spec.rb +0 -18
- data/spec/code/unary_minus_spec.rb +0 -13
- data/spec/code/while_spec.rb +0 -18
- data/template-ruby.gemspec +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe75dad140254848b0688e73fc0361003867abf259e3efd57eb8709072808461
|
4
|
+
data.tar.gz: 592c2cf305d38613461981802bb63a9288b2a7bac862220cb818c4e1b9e1922c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b61958d200d6d2e2f1a0ed41e93a992320e3b224c8e65f0cfb3e3ff35ad78f07b930234d2ec190cad4384b1127a347d7651597831033bf1b14bda18a488f6f41
|
7
|
+
data.tar.gz: eb44f9c8522a461b4b40e7b369a9ac0653ff30c710c2c5395840e3e5b8181596ce4553fa7033370636e6974dd79e2e326e0e7bf6d0f2061be84468d30b670b49
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.3.0
|
data/.tool-versions
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby 3.
|
1
|
+
ruby 3.3.0
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,49 +1,38 @@
|
|
1
|
-
GIT
|
2
|
-
remote: https://github.com/dorianmariefr/syntax_tree.git
|
3
|
-
revision: f7c9dc36a3d60c90a8d2411f123913b37319dc11
|
4
|
-
specs:
|
5
|
-
syntax_tree (5.0.1)
|
6
|
-
prettier_print (>= 1.1.0)
|
7
|
-
|
8
1
|
PATH
|
9
2
|
remote: .
|
10
3
|
specs:
|
11
|
-
|
12
|
-
zeitwerk (~> 2)
|
13
|
-
template-ruby (0.5.6)
|
4
|
+
language-ruby (0.6.0)
|
14
5
|
zeitwerk (~> 2)
|
15
6
|
|
16
7
|
GEM
|
17
8
|
remote: https://rubygems.org/
|
18
9
|
specs:
|
19
10
|
diff-lcs (1.5.0)
|
20
|
-
prettier_print (1.1.0)
|
21
11
|
rspec (3.12.0)
|
22
12
|
rspec-core (~> 3.12.0)
|
23
13
|
rspec-expectations (~> 3.12.0)
|
24
14
|
rspec-mocks (~> 3.12.0)
|
25
|
-
rspec-core (3.12.
|
15
|
+
rspec-core (3.12.2)
|
26
16
|
rspec-support (~> 3.12.0)
|
27
|
-
rspec-expectations (3.12.
|
17
|
+
rspec-expectations (3.12.3)
|
28
18
|
diff-lcs (>= 1.2.0, < 2.0)
|
29
19
|
rspec-support (~> 3.12.0)
|
30
|
-
rspec-mocks (3.12.
|
20
|
+
rspec-mocks (3.12.6)
|
31
21
|
diff-lcs (>= 1.2.0, < 2.0)
|
32
22
|
rspec-support (~> 3.12.0)
|
33
|
-
rspec-support (3.12.
|
34
|
-
|
35
|
-
zeitwerk (2.6.6)
|
23
|
+
rspec-support (3.12.1)
|
24
|
+
zeitwerk (2.6.12)
|
36
25
|
|
37
26
|
PLATFORMS
|
38
|
-
arm64-darwin-21
|
39
27
|
arm64-darwin-22
|
28
|
+
arm64-darwin-23
|
40
29
|
|
41
30
|
DEPENDENCIES
|
42
|
-
|
31
|
+
language-ruby!
|
43
32
|
rspec
|
44
|
-
|
45
|
-
|
46
|
-
|
33
|
+
|
34
|
+
RUBY VERSION
|
35
|
+
ruby 3.3.0p0
|
47
36
|
|
48
37
|
BUNDLED WITH
|
49
|
-
2.
|
38
|
+
2.4.21
|
data/README.md
CHANGED
@@ -1,103 +1,3 @@
|
|
1
|
-
#
|
1
|
+
# language-ruby
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
See [templatelang.com](https://templatelang.com) for the full documentation and
|
6
|
-
live code editing.
|
7
|
-
|
8
|
-
## The programming language
|
9
|
-
|
10
|
-
Hi, I'm [Dorian Marié](https://dorianmarie.fr), I created Template to let users of my websites provide templates to customize their experience.
|
11
|
-
|
12
|
-
Template is meant to be:
|
13
|
-
|
14
|
-
- **Simple**: `Hello` and `Hello {name}`
|
15
|
-
- **Safe**: Can be provided user input
|
16
|
-
- **Powerful**: Functions, object-oriented, built-in methods
|
17
|
-
|
18
|
-
Template is currently written in Ruby and embeddable as a Ruby gem.
|
19
|
-
|
20
|
-
## Install
|
21
|
-
|
22
|
-
### As a command line tool:
|
23
|
-
|
24
|
-
```bash
|
25
|
-
$ gem install template-ruby
|
26
|
-
$ template --input "Hello {name}" --context '{ name: "Dorian" }'
|
27
|
-
Hello Dorian
|
28
|
-
$ template --input "1 + 2 = {1 + 2}"
|
29
|
-
1 + 2 = 3
|
30
|
-
```
|
31
|
-
|
32
|
-
### As a Ruby gem:
|
33
|
-
|
34
|
-
In a `Gemfile`:
|
35
|
-
|
36
|
-
```ruby
|
37
|
-
gem "template-ruby"
|
38
|
-
```
|
39
|
-
|
40
|
-
Then `$ bundle install`
|
41
|
-
|
42
|
-
Then you can use Template like:
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
Template.render("Hello {name}", '{ name: "Dorian" }')
|
46
|
-
# => "Hello Dorian"
|
47
|
-
Template.render("1 + 2 = {1 + 2}")
|
48
|
-
# => "1 + 2 = 3"
|
49
|
-
Template.render(input, context, io: StringIO.new, timeout: 10)
|
50
|
-
```
|
51
|
-
|
52
|
-
The context is a sub-language called Code, you can use it like:
|
53
|
-
|
54
|
-
```ruby
|
55
|
-
Code.evaluate("1 + 2") # => 3
|
56
|
-
```
|
57
|
-
|
58
|
-
## Future work
|
59
|
-
|
60
|
-
- Extend standard library
|
61
|
-
- Global methods from Ruby, e.g. `{markdown "**bold**"}`
|
62
|
-
- Object methods from Ruby, e.g. `{"**bold**".markdown}`
|
63
|
-
- Classes, e.g. `{class User end}`
|
64
|
-
- Write JavaScript version
|
65
|
-
- Write Crystal version
|
66
|
-
|
67
|
-
## Contributing
|
68
|
-
|
69
|
-
Feel free to open [issues](https://github.com/dorianmariefr/template-ruby/issues),
|
70
|
-
and [pull requests](https://github.com/dorianmariefr/template-ruby/pulls).
|
71
|
-
|
72
|
-
To develop locally:
|
73
|
-
|
74
|
-
```text
|
75
|
-
$ git clone https://github.com/dorianmariefr/template-ruby
|
76
|
-
$ cd template-ruby
|
77
|
-
$ bundle
|
78
|
-
$ rspec
|
79
|
-
$ bin/template -i docs/...
|
80
|
-
```
|
81
|
-
|
82
|
-
## Credits
|
83
|
-
|
84
|
-
Thanks to [thoughtbot](https://thoughtbot.com) who let me work on this programming
|
85
|
-
language as a Friday project.
|
86
|
-
|
87
|
-
Thanks to [Kaspar Schiess](https://github.com/kschiess) who made
|
88
|
-
[Parslet](https://kschiess.github.io/parslet/), the gem that helped me write the parser.
|
89
|
-
|
90
|
-
Inspiration from [Ruby](https://www.ruby-lang.org/en/) and
|
91
|
-
[Liquid](https://shopify.github.io/liquid/).
|
92
|
-
|
93
|
-
## License
|
94
|
-
|
95
|
-
MIT
|
96
|
-
|
97
|
-
Copyright 2022 Dorian Marié <dorian@dorianmarie.fr>
|
98
|
-
|
99
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
100
|
-
|
101
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
102
|
-
|
103
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
3
|
+
A Parsing Expression Grammar (PEG) for making parsers
|
data/language-ruby.gemspec
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "English"
|
4
|
+
require_relative "lib/language/version"
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = "language-ruby"
|
5
|
-
s.version = ::
|
8
|
+
s.version = ::Language::Version
|
6
9
|
s.summary = "A Parsing Expression Grammar (PEG) for making parsers"
|
7
10
|
s.description = s.summary
|
8
11
|
s.authors = ["Dorian Marié"]
|
9
12
|
s.email = "dorian@dorianmarie.fr"
|
10
|
-
s.files = `git ls-files`.split(
|
11
|
-
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
13
|
+
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
12
14
|
s.require_paths = ["lib"]
|
13
|
-
s.homepage = "https://github.com/dorianmariefr/
|
15
|
+
s.homepage = "https://github.com/dorianmariefr/language-ruby"
|
14
16
|
s.license = "MIT"
|
15
17
|
|
16
18
|
s.add_dependency "zeitwerk", "~> 2"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class Absent < Atom
|
6
|
+
def initialize(parent: nil)
|
7
|
+
@parent = parent
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse(parser)
|
11
|
+
clone =
|
12
|
+
Parser.new(
|
13
|
+
root: self,
|
14
|
+
input: parser.input,
|
15
|
+
cursor: parser.cursor,
|
16
|
+
buffer: parser.buffer
|
17
|
+
)
|
18
|
+
@parent&.parse(clone)
|
19
|
+
rescue Parser::Interuption
|
20
|
+
else
|
21
|
+
raise Parser::Interuption.new(parser, self)
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
@parent ? "(#{@parent}).absent" : "absent"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class Aka < Atom
|
6
|
+
def initialize(name:, parent:)
|
7
|
+
@name = name
|
8
|
+
@parent = parent
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse(parser)
|
12
|
+
clone =
|
13
|
+
Parser.new(root: self, input: parser.input, cursor: parser.cursor)
|
14
|
+
|
15
|
+
@parent.parse(clone)
|
16
|
+
|
17
|
+
if clone.output?
|
18
|
+
parser.output = Output.new(@name => clone.output)
|
19
|
+
else
|
20
|
+
parser.output[@name] = Output.new(clone.buffer)
|
21
|
+
parser.buffer = ""
|
22
|
+
end
|
23
|
+
|
24
|
+
parser.cursor = clone.cursor
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
@parent ? "#{@parent}.aka(#{@name.inspect})" : "aka(#{@name.inspect})"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class And < Atom
|
6
|
+
def initialize(left:, right:)
|
7
|
+
@left = left
|
8
|
+
@right = right
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse(parser)
|
12
|
+
@left.parse(parser)
|
13
|
+
right_clone =
|
14
|
+
Parser.new(
|
15
|
+
root: self,
|
16
|
+
input: parser.input,
|
17
|
+
cursor: parser.cursor,
|
18
|
+
buffer: parser.buffer
|
19
|
+
)
|
20
|
+
@right.parse(right_clone)
|
21
|
+
parser.cursor = right_clone.cursor
|
22
|
+
parser.buffer = right_clone.buffer
|
23
|
+
|
24
|
+
parser.output.merge(right_clone.output)
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
"#{@left} >> #{@right}".to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class Ignore < Atom
|
6
|
+
def initialize(parent:)
|
7
|
+
@parent = parent
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse(parser)
|
11
|
+
clone =
|
12
|
+
Parser.new(
|
13
|
+
root: self,
|
14
|
+
input: parser.input,
|
15
|
+
cursor: parser.cursor,
|
16
|
+
buffer: parser.buffer
|
17
|
+
)
|
18
|
+
@parent.parse(clone)
|
19
|
+
parser.cursor = clone.cursor
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
"#{@parent}.ignore"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class Maybe < Atom
|
6
|
+
def initialize(parent:)
|
7
|
+
@parent = parent
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse(parser)
|
11
|
+
clone =
|
12
|
+
Parser.new(
|
13
|
+
root: self,
|
14
|
+
input: parser.input,
|
15
|
+
cursor: parser.cursor,
|
16
|
+
buffer: parser.buffer
|
17
|
+
)
|
18
|
+
|
19
|
+
@parent.parse(clone)
|
20
|
+
rescue Parser::Interuption
|
21
|
+
else
|
22
|
+
parser.buffer = clone.buffer
|
23
|
+
parser.cursor = clone.cursor
|
24
|
+
parser.output = clone.output
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
@parent ? "#{@parent}.maybe" : "maybe"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class Or < Atom
|
6
|
+
def initialize(left:, right:)
|
7
|
+
@left = left
|
8
|
+
@right = right
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse(parser)
|
12
|
+
left_clone =
|
13
|
+
Parser.new(
|
14
|
+
root: self,
|
15
|
+
input: parser.input,
|
16
|
+
cursor: parser.cursor,
|
17
|
+
buffer: parser.buffer
|
18
|
+
)
|
19
|
+
|
20
|
+
right_clone =
|
21
|
+
Parser.new(
|
22
|
+
root: self,
|
23
|
+
input: parser.input,
|
24
|
+
cursor: parser.cursor,
|
25
|
+
buffer: parser.buffer
|
26
|
+
)
|
27
|
+
|
28
|
+
begin
|
29
|
+
@left.parse(left_clone)
|
30
|
+
parser.cursor = left_clone.cursor
|
31
|
+
parser.buffer = left_clone.buffer
|
32
|
+
parser.output.merge(left_clone.output)
|
33
|
+
rescue Parser::Interuption
|
34
|
+
@right.parse(right_clone)
|
35
|
+
parser.cursor = right_clone.cursor
|
36
|
+
parser.buffer = right_clone.buffer
|
37
|
+
parser.output.merge(right_clone.output)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_s
|
42
|
+
"((#{@left}) | (#{@right}))"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class Repeat < Atom
|
6
|
+
def initialize(parent: nil, minimum: 0, maximum: nil)
|
7
|
+
@parent = parent
|
8
|
+
@minimum = minimum
|
9
|
+
@maximum = maximum
|
10
|
+
end
|
11
|
+
|
12
|
+
def parse(parser)
|
13
|
+
return unless @parent
|
14
|
+
|
15
|
+
@minimum.times { match(parser) }
|
16
|
+
if @maximum.nil?
|
17
|
+
begin
|
18
|
+
loop { match(parser) }
|
19
|
+
rescue Parser::Interuption
|
20
|
+
end
|
21
|
+
else
|
22
|
+
begin
|
23
|
+
(@maximum - @minimum).times { match(parser) }
|
24
|
+
rescue Parser::Interuption
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
minimum = @minimum.zero? ? "" : @minimum.to_s
|
31
|
+
maximum = @maximum.nil? ? "" : ", #{@maximum}"
|
32
|
+
parenthesis =
|
33
|
+
(minimum.empty? && maximum.empty?) ? "" : "(#{minimum}#{maximum})"
|
34
|
+
|
35
|
+
@parent ? "(#{@parent}).repeat#{parenthesis}" : "repeat#{parenthesis}"
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def match(parser)
|
41
|
+
clone =
|
42
|
+
Parser.new(
|
43
|
+
root: self,
|
44
|
+
input: parser.input,
|
45
|
+
cursor: parser.cursor,
|
46
|
+
buffer: parser.buffer
|
47
|
+
)
|
48
|
+
|
49
|
+
@parent.parse(clone)
|
50
|
+
|
51
|
+
parser.cursor = clone.cursor
|
52
|
+
parser.buffer = clone.buffer
|
53
|
+
parser.output << clone.output
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class Str < Atom
|
6
|
+
def initialize(string:)
|
7
|
+
@string = string
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse(parser)
|
11
|
+
raise Parser::Str::NotFound.new(parser, @string) unless parser.next?(@string)
|
12
|
+
|
13
|
+
parser.consume(@string.size)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"str(#{@string.inspect})"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Language
|
4
|
+
class Atom
|
5
|
+
class Then < Atom
|
6
|
+
def initialize(parent:, block:)
|
7
|
+
@parent = parent
|
8
|
+
@block = block
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse(parser)
|
12
|
+
@parent.parse(parser)
|
13
|
+
parser.output = Output.from_raw(@block.call(parser.output.to_raw))
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"(#{@parent}).then {}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|