Module: TRuby::ParserCombinator::DSL
- Included in:
- DeclarationParser, TypeParser
- Defined in:
- lib/t_ruby/parser_combinator.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(&block) ⇒ 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
591 592 593 |
# File 'lib/t_ruby/parser_combinator.rb', line 591 def alphanumeric satisfy("alphanumeric") { |c| c =~ /[a-zA-Z0-9]/ } end |
#chainl(term, op) ⇒ Object
Chain for left-associative operators
641 642 643 |
# File 'lib/t_ruby/parser_combinator.rb', line 641 def chainl(term, op) ChainLeft.new(term, op) end |
#char(c) ⇒ Object
550 551 552 |
# File 'lib/t_ruby/parser_combinator.rb', line 550 def char(c) Literal.new(c) end |
#choice(*parsers) ⇒ Object
574 575 576 |
# File 'lib/t_ruby/parser_combinator.rb', line 574 def choice(*parsers) Choice.new(*parsers) end |
#digit ⇒ Object
Common character parsers
583 584 585 |
# File 'lib/t_ruby/parser_combinator.rb', line 583 def digit satisfy("digit") { |c| c =~ /[0-9]/ } end |
#eof ⇒ Object
558 559 560 |
# File 'lib/t_ruby/parser_combinator.rb', line 558 def eof EndOfInput.new end |
#fail(message) ⇒ Object
566 567 568 |
# File 'lib/t_ruby/parser_combinator.rb', line 566 def fail() Fail.new() end |
#float ⇒ Object
624 625 626 |
# File 'lib/t_ruby/parser_combinator.rb', line 624 def float regex(/\-?\d+\.\d+/, "float").map(&:to_f) end |
#identifier ⇒ Object
611 612 613 614 615 |
# File 'lib/t_ruby/parser_combinator.rb', line 611 def identifier (letter >> (alphanumeric | char("_")).many).map do |(first, rest)| first + rest.join end end |
#integer ⇒ Object
617 618 619 620 621 622 |
# File 'lib/t_ruby/parser_combinator.rb', line 617 def integer (char("-").optional >> digit.many1).map do |(sign, digits)| num = digits.join.to_i sign ? -num : num end end |
#lazy(&block) ⇒ Object
570 571 572 |
# File 'lib/t_ruby/parser_combinator.rb', line 570 def lazy(&block) Lazy.new(&block) end |
#letter ⇒ Object
587 588 589 |
# File 'lib/t_ruby/parser_combinator.rb', line 587 def letter satisfy("letter") { |c| c =~ /[a-zA-Z]/ } end |
#lexeme(parser) ⇒ Object
Skip whitespace around parser
636 637 638 |
# File 'lib/t_ruby/parser_combinator.rb', line 636 def lexeme(parser) (spaces >> parser << spaces).map { |(_, val)| val } end |
#literal(str) ⇒ Object
538 539 540 |
# File 'lib/t_ruby/parser_combinator.rb', line 538 def literal(str) Literal.new(str) end |
#newline ⇒ Object
607 608 609 |
# File 'lib/t_ruby/parser_combinator.rb', line 607 def newline char("\n") | string("\r\n") end |
#pure(value) ⇒ Object
562 563 564 |
# File 'lib/t_ruby/parser_combinator.rb', line 562 def pure(value) Pure.new(value) end |
#quoted_string(quote = '"') ⇒ Object
628 629 630 631 632 633 |
# File 'lib/t_ruby/parser_combinator.rb', line 628 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
542 543 544 |
# File 'lib/t_ruby/parser_combinator.rb', line 542 def regex(pattern, description = nil) Regex.new(pattern, description) end |
#satisfy(description = "character", &predicate) ⇒ Object
546 547 548 |
# File 'lib/t_ruby/parser_combinator.rb', line 546 def satisfy(description = "character", &predicate) Satisfy.new(predicate, description) end |
#sequence(*parsers) ⇒ Object
578 579 580 |
# File 'lib/t_ruby/parser_combinator.rb', line 578 def sequence(*parsers) parsers.reduce { |acc, p| acc >> p } end |
#spaces ⇒ Object
599 600 601 |
# File 'lib/t_ruby/parser_combinator.rb', line 599 def spaces whitespace.many.map { |chars| chars.join } end |
#spaces1 ⇒ Object
603 604 605 |
# File 'lib/t_ruby/parser_combinator.rb', line 603 def spaces1 whitespace.many1.map { |chars| chars.join } end |
#string(str) ⇒ Object
554 555 556 |
# File 'lib/t_ruby/parser_combinator.rb', line 554 def string(str) Literal.new(str) end |
#whitespace ⇒ Object
595 596 597 |
# File 'lib/t_ruby/parser_combinator.rb', line 595 def whitespace satisfy("whitespace") { |c| c =~ /\s/ } end |