Module: TRuby::ParserCombinator::DSL
- Included in:
- DeclarationParser, TypeParser
- Defined in:
- lib/t_ruby/parser_combinator/dsl.rb
Overview
DSL Module - Convenience methods
Instance Method Summary collapse
- #alphanumeric ⇒ Object
-
#chainl(term, op) ⇒ Object
Chain for left-associative operators.
- #char(c) ⇒ Object
- #choice(*parsers) ⇒ Object
-
#digit ⇒ Object
Common character parsers.
- #eof ⇒ Object
- #fail(message) ⇒ Object
- #float ⇒ Object
- #identifier ⇒ Object
- #integer ⇒ Object
- #lazy ⇒ Object
- #letter ⇒ Object
-
#lexeme(parser) ⇒ Object
Skip whitespace around parser.
- #literal(str) ⇒ Object
- #newline ⇒ Object
- #pure(value) ⇒ Object
- #quoted_string(quote = '"') ⇒ Object
- #regex(pattern, description = nil) ⇒ Object
- #satisfy(description = "character", &predicate) ⇒ Object
- #sequence(*parsers) ⇒ Object
- #spaces ⇒ Object
- #spaces1 ⇒ Object
- #string(str) ⇒ Object
- #whitespace ⇒ Object
Instance Method Details
#alphanumeric ⇒ Object
60 61 62 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 60 def alphanumeric satisfy("alphanumeric") { |c| c =~ /[a-zA-Z0-9]/ } end |
#chainl(term, op) ⇒ Object
Chain for left-associative operators
110 111 112 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 110 def chainl(term, op) ChainLeft.new(term, op) end |
#char(c) ⇒ Object
19 20 21 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 19 def char(c) Literal.new(c) end |
#choice(*parsers) ⇒ Object
43 44 45 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 43 def choice(*parsers) Choice.new(*parsers) end |
#digit ⇒ Object
Common character parsers
52 53 54 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 52 def digit satisfy("digit") { |c| c =~ /[0-9]/ } end |
#eof ⇒ Object
27 28 29 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 27 def eof EndOfInput.new end |
#fail(message) ⇒ Object
35 36 37 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 35 def fail() Fail.new() end |
#float ⇒ Object
93 94 95 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 93 def float regex(/-?\d+\.\d+/, "float").map(&:to_f) end |
#identifier ⇒ Object
80 81 82 83 84 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 80 def identifier (letter >> (alphanumeric | char("_")).many).map do |(first, rest)| first + rest.join end end |
#integer ⇒ Object
86 87 88 89 90 91 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 86 def integer (char("-").optional >> digit.many1).map do |(sign, digits)| num = digits.join.to_i sign ? -num : num end end |
#lazy ⇒ Object
39 40 41 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 39 def lazy(&) Lazy.new(&) end |
#letter ⇒ Object
56 57 58 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 56 def letter satisfy("letter") { |c| c =~ /[a-zA-Z]/ } end |
#lexeme(parser) ⇒ Object
Skip whitespace around parser
105 106 107 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 105 def lexeme(parser) (spaces >> parser << spaces).map { |(_, val)| val } end |
#literal(str) ⇒ Object
7 8 9 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 7 def literal(str) Literal.new(str) end |
#newline ⇒ Object
76 77 78 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 76 def newline char("\n") | string("\r\n") end |
#pure(value) ⇒ Object
31 32 33 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 31 def pure(value) Pure.new(value) end |
#quoted_string(quote = '"') ⇒ Object
97 98 99 100 101 102 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 97 def quoted_string(quote = '"') content = satisfy("string character") { |c| c != quote && c != "\\" } escape = (char("\\") >> satisfy("escape char")).map { |(_bs, c)| c } (char(quote) >> (content | escape).many.map(&:join) << char(quote)).map { |(_, str)| str } end |
#regex(pattern, description = nil) ⇒ Object
11 12 13 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 11 def regex(pattern, description = nil) Regex.new(pattern, description) end |
#satisfy(description = "character", &predicate) ⇒ Object
15 16 17 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 15 def satisfy(description = "character", &predicate) Satisfy.new(predicate, description) end |
#sequence(*parsers) ⇒ Object
47 48 49 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 47 def sequence(*parsers) parsers.reduce { |acc, p| acc >> p } end |
#spaces ⇒ Object
68 69 70 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 68 def spaces whitespace.many.map(&:join) end |
#spaces1 ⇒ Object
72 73 74 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 72 def spaces1 whitespace.many1.map(&:join) end |
#string(str) ⇒ Object
23 24 25 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 23 def string(str) Literal.new(str) end |
#whitespace ⇒ Object
64 65 66 |
# File 'lib/t_ruby/parser_combinator/dsl.rb', line 64 def whitespace satisfy("whitespace") { |c| c =~ /\s/ } end |