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

Instance Method Details

#alphanumericObject



595
596
597
# File 'lib/t_ruby/parser_combinator.rb', line 595

def alphanumeric
  satisfy("alphanumeric") { |c| c =~ /[a-zA-Z0-9]/ }
end

#chainl(term, op) ⇒ Object

Chain for left-associative operators



645
646
647
# File 'lib/t_ruby/parser_combinator.rb', line 645

def chainl(term, op)
  ChainLeft.new(term, op)
end

#char(c) ⇒ Object



554
555
556
# File 'lib/t_ruby/parser_combinator.rb', line 554

def char(c)
  Literal.new(c)
end

#choice(*parsers) ⇒ Object



578
579
580
# File 'lib/t_ruby/parser_combinator.rb', line 578

def choice(*parsers)
  Choice.new(*parsers)
end

#digitObject

Common character parsers



587
588
589
# File 'lib/t_ruby/parser_combinator.rb', line 587

def digit
  satisfy("digit") { |c| c =~ /[0-9]/ }
end

#eofObject



562
563
564
# File 'lib/t_ruby/parser_combinator.rb', line 562

def eof
  EndOfInput.new
end

#fail(message) ⇒ Object



570
571
572
# File 'lib/t_ruby/parser_combinator.rb', line 570

def fail(message)
  Fail.new(message)
end

#floatObject



628
629
630
# File 'lib/t_ruby/parser_combinator.rb', line 628

def float
  regex(/-?\d+\.\d+/, "float").map(&:to_f)
end

#identifierObject



615
616
617
618
619
# File 'lib/t_ruby/parser_combinator.rb', line 615

def identifier
  (letter >> (alphanumeric | char("_")).many).map do |(first, rest)|
    first + rest.join
  end
end

#integerObject



621
622
623
624
625
626
# File 'lib/t_ruby/parser_combinator.rb', line 621

def integer
  (char("-").optional >> digit.many1).map do |(sign, digits)|
    num = digits.join.to_i
    sign ? -num : num
  end
end

#lazyObject



574
575
576
# File 'lib/t_ruby/parser_combinator.rb', line 574

def lazy(&)
  Lazy.new(&)
end

#letterObject



591
592
593
# File 'lib/t_ruby/parser_combinator.rb', line 591

def letter
  satisfy("letter") { |c| c =~ /[a-zA-Z]/ }
end

#lexeme(parser) ⇒ Object

Skip whitespace around parser



640
641
642
# File 'lib/t_ruby/parser_combinator.rb', line 640

def lexeme(parser)
  (spaces >> parser << spaces).map { |(_, val)| val }
end

#literal(str) ⇒ Object



542
543
544
# File 'lib/t_ruby/parser_combinator.rb', line 542

def literal(str)
  Literal.new(str)
end

#newlineObject



611
612
613
# File 'lib/t_ruby/parser_combinator.rb', line 611

def newline
  char("\n") | string("\r\n")
end

#pure(value) ⇒ Object



566
567
568
# File 'lib/t_ruby/parser_combinator.rb', line 566

def pure(value)
  Pure.new(value)
end

#quoted_string(quote = '"') ⇒ Object



632
633
634
635
636
637
# File 'lib/t_ruby/parser_combinator.rb', line 632

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



546
547
548
# File 'lib/t_ruby/parser_combinator.rb', line 546

def regex(pattern, description = nil)
  Regex.new(pattern, description)
end

#satisfy(description = "character", &predicate) ⇒ Object



550
551
552
# File 'lib/t_ruby/parser_combinator.rb', line 550

def satisfy(description = "character", &predicate)
  Satisfy.new(predicate, description)
end

#sequence(*parsers) ⇒ Object



582
583
584
# File 'lib/t_ruby/parser_combinator.rb', line 582

def sequence(*parsers)
  parsers.reduce { |acc, p| acc >> p }
end

#spacesObject



603
604
605
# File 'lib/t_ruby/parser_combinator.rb', line 603

def spaces
  whitespace.many.map(&:join)
end

#spaces1Object



607
608
609
# File 'lib/t_ruby/parser_combinator.rb', line 607

def spaces1
  whitespace.many1.map(&:join)
end

#string(str) ⇒ Object



558
559
560
# File 'lib/t_ruby/parser_combinator.rb', line 558

def string(str)
  Literal.new(str)
end

#whitespaceObject



599
600
601
# File 'lib/t_ruby/parser_combinator.rb', line 599

def whitespace
  satisfy("whitespace") { |c| c =~ /\s/ }
end