pratt_parser 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2be786fcdd7e3a6860fd2218ae404036a6de85e1
4
+ data.tar.gz: b0684865ee6fcbb04ef26544819e43598e2dfa3a
5
+ SHA512:
6
+ metadata.gz: 6d7a691fa927a0e0095827cf1abebdf4ac8102a12774b6b60a93693eef495beabe79ef4b6f4885d99ccecf5928957684f7eb1fdc377b032300eec099a8943cc9
7
+ data.tar.gz: 3d8549f09a0d0b74b45238f390b0124fe5e7cbcaf6e9e25a13513f45701212945044add373e623e8064b005bdb6f2cc313ebfc65dd9700db824b20dec28fcde5
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 F Thomas May
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ Pratt Parser
2
+ ============
3
+
4
+ A Pratt Parser. Construct it with a Lexer that returns tokens that
5
+ define the language to be parsed and combines terms.
6
+
7
+ Pratt parsers are like recursive descent parsers but instead of having
8
+ to code a function for each production you just code up some token
9
+ objects which define their own prededence and associativity. So it's
10
+ simple to add new tokens without having to rewrite a bunch of
11
+ recursive descent functions.
12
+
13
+ Pratt parsers are also more efficient than recursive descent parsers
14
+ since they don't need to recurse all the way down to the bottom level
15
+ to figure out what to do.
16
+
17
+ http://javascript.crockford.com/tdop/tdop.html
18
+
19
+ http://effbot.org/zone/simple-top-down-parsing.htm
20
+
21
+ http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ task :gem do
2
+ sh "gem build pratt_parser.gemspec"
3
+ end
4
+
5
+ task :install => :gem do
6
+ sh "gem install pratt_parser"
7
+ end
8
+
9
+ task :clean do
10
+ sh "rm *.gem"
11
+ end
@@ -0,0 +1,84 @@
1
+ # A Pratt parser. Similar to a recursive decent parser but instead of
2
+ # coding a function for each production, the syntax is coded in a set
3
+ # of token objects that are yielded by the lexer. New operators and
4
+ # statements can be slipped in to the language with the proper
5
+ # precedence by adding new token objects to the lexer without altering
6
+ # the code for existing tokens. Pretty cool.
7
+ #
8
+ # lexer is an enumerator with an each method that returns token objects
9
+ # with three methods:
10
+ # lbp: return the operator precedence. Higher numbers bind more tightly.
11
+ # nud(parser): called when the token is the first token in an expression,
12
+ # including a recursive call to expresssion (i.e., subexpression). For
13
+ # Example, this would be called for a unary operator, a literal, or for
14
+ # the "if" in the construct "if <cond> then <expr>".
15
+ # It is the token's responsibility to call parser.expression, parser.expect,
16
+ # and/or parser.if? to handle the remainder of the expression, if any.
17
+ # led(parser, left): called when the token is preceeded by a subexpression,
18
+ # left. The token may be postfix or infix.
19
+ # It is the token's responsibility to call parser.expression, parser.expect,
20
+ # and/or parser.if? to handle the remainder of the expression, if any,
21
+ # and combine it with left.
22
+ # Only lbp is mandatory. nud and led will be called only when necessary, if
23
+ # ever.
24
+ # nud and lcd can call parser.expression(rbp) to recursively parse the
25
+ # right expression. rbp should be the token's lbp for left-associativity,
26
+ # lbp-1 for right.
27
+ #
28
+ # PrattParser.new(lexer).eval will return the result of the parse.
29
+ #
30
+ # Syntax errors aren't handled at the moment and will cause ridiculous
31
+ # exceptions to be raised such as NoMethodError.
32
+
33
+ # http://javascript.crockford.com/tdop/tdop.html
34
+ # http://effbot.org/zone/simple-top-down-parsing.htm
35
+ # http://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-expression-parsing-made-easy/
36
+
37
+ class PrattParser
38
+ def initialize(lexer)
39
+ @lexer = Enumerator.new do |y|
40
+ lexer.each do |token|
41
+ y << token
42
+ end
43
+ y << EndToken.new
44
+ end
45
+
46
+ @token = nil
47
+ end
48
+
49
+ def eval
50
+ @token = @lexer.next
51
+ expression(0)
52
+ end
53
+
54
+ def expression(rbp)
55
+ t = @token
56
+ @token = @lexer.next
57
+ left = t.nud(self)
58
+ while rbp < @token.lbp
59
+ t = @token
60
+ @token = @lexer.next
61
+ left = t.led(self, left)
62
+ end
63
+ left
64
+ end
65
+
66
+ def expect(expected_token_class)
67
+ if @token.class != expected_token_class
68
+ raise "Expected #{expected_token_class}, got #{@token.class}"
69
+ end
70
+ @token = @lexer.next
71
+ end
72
+
73
+ def if?(token_class)
74
+ if @token.class == token_class
75
+ @token = @lexer.next
76
+ end
77
+ end
78
+
79
+ class EndToken
80
+ def lbp
81
+ 0
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.description = "A Pratt parser. Create token objects to define your language."
3
+ gem.summary = "A Pratt parser."
4
+ gem.authors = ["Tom May"]
5
+ gem.email = ["tom@tommay.net"]
6
+ gem.homepage = "https://github.com/tommay/pratt_parser"
7
+ gem.files = `git ls-files | egrep -v '^example'`.split("\n")
8
+ gem.test_files = `git ls-files -- spec/*`.split("\n")
9
+ gem.name = "pratt_parser"
10
+ gem.require_paths = ["lib"]
11
+ gem.version = "0.0.0"
12
+ gem.license = "MIT"
13
+ # gem.required_ruby_version = '>= 2.1.0'
14
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pratt_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom May
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Pratt parser. Create token objects to define your language.
14
+ email:
15
+ - tom@tommay.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - LICENSE.md
22
+ - README.md
23
+ - Rakefile
24
+ - lib/pratt_parser.rb
25
+ - pratt_parser.gemspec
26
+ homepage: https://github.com/tommay/pratt_parser
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.4.5
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A Pratt parser.
50
+ test_files: []