class FunctionalParser

Constants

VERSION

Public Class Methods

alpha() click to toggle source
# File lib/functional_parser.rb, line 98
def self.alpha
  lower | upper
end
alphanum() click to toggle source
# File lib/functional_parser.rb, line 102
def self.alphanum
  alpha | digit
end
append(parser1, parser2) click to toggle source
# File lib/functional_parser.rb, line 72
def self.append(parser1, parser2)
  parser1 >> proc{|x|
    parser2 >> proc{|y|
      ret(x + y)
    }
  }
end
char(char) click to toggle source
# File lib/functional_parser.rb, line 106
def self.char(char)
  sat{|c| c == char}
end
digit() click to toggle source
# File lib/functional_parser.rb, line 86
def self.digit
  sat{|c| "0" <= c && c <= "9"}
end
either(parser1, parser2) click to toggle source
# File lib/functional_parser.rb, line 59
def self.either(parser1, parser2)
  new{|inp|
    case result = parser1.parse(inp)
    when Failed
      parser2.parse(inp)
    when Succeeded
      result
    else
      raise "error."
    end
  }
end
failure() click to toggle source
# File lib/functional_parser.rb, line 38
def self.failure
  new{|inp| Failed.new}
end
ident() click to toggle source
# File lib/functional_parser.rb, line 134
def self.ident
  many1(alphanum) >> proc{|cs|
    ret(cs.inject(&:+))
  }
end
identifier() click to toggle source
# File lib/functional_parser.rb, line 162
def self.identifier
  token(ident)
end
item() click to toggle source
# File lib/functional_parser.rb, line 42
def self.item
  new{|inp| inp.size == 0 ? Failed.new : Succeeded.new(inp[0], inp[1, inp.size - 1])}
end
lower() click to toggle source
# File lib/functional_parser.rb, line 90
def self.lower
  sat{|c| "a" <= c && c <= "z"}
end
many(parser) click to toggle source
# File lib/functional_parser.rb, line 122
def self.many(parser)
  many1(parser) | ret([])
end
many1(parser) click to toggle source
# File lib/functional_parser.rb, line 126
def self.many1(parser)
  parser >> proc{|x|
    many(parser) >> proc{|xs|
      ret([x] + xs)
    }
  }
end
nat() click to toggle source
# File lib/functional_parser.rb, line 140
def self.nat
  many1(digit) >> proc{|cs|
    ret(cs.inject(&:+).to_i)
  }
end
natural() click to toggle source
# File lib/functional_parser.rb, line 166
def self.natural
  token(nat)
end
new(&proc) click to toggle source
# File lib/functional_parser.rb, line 14
def initialize(&proc)
  @f = proc
end
ret(something) click to toggle source
# File lib/functional_parser.rb, line 34
def self.ret(something)
  new{|inp| Succeeded.new(something, inp)}
end
sat(&proc) click to toggle source
# File lib/functional_parser.rb, line 80
def self.sat(&proc)
  item >> proc{|c|
    proc.call(c) ? ret(c) : failure
  }
end
so(parser, &proc) click to toggle source
# File lib/functional_parser.rb, line 46
def self.so(parser, &proc)
  new{|inp|
    case result = parser.parse(inp)
    when Failed
      result
    when Succeeded
      proc.call(result.parsed).parse(result.rest)
    else
      raise "error."
    end
  }
end
string(str) click to toggle source
# File lib/functional_parser.rb, line 110
def self.string(str)
  if str.size == 0
    ret(str)
  else
    char(str[0]) >> proc{|c|
      string(str[1, str.size - 1]) >> proc{
        ret(str)
      }
    }
  end
end
token(parser) click to toggle source
# File lib/functional_parser.rb, line 152
def self.token(parser)
  whitespace >> proc{
    parser >> proc{|x|
      whitespace >> proc{
        ret(x)
      }
    }
  }
end
upper() click to toggle source
# File lib/functional_parser.rb, line 94
def self.upper
  sat{|c| "A" <= c && c <= "Z"}
end
whitespace() click to toggle source
# File lib/functional_parser.rb, line 146
def self.whitespace
  many(char("\s") | char("\n") | char("\t")) >> proc{
    ret(nil)
  }
end

Public Instance Methods

+(other) click to toggle source
# File lib/functional_parser.rb, line 30
def +(other)
  FunctionalParser.append(self, other)
end
>>(proc) click to toggle source
# File lib/functional_parser.rb, line 22
def >>(proc)
  FunctionalParser.so(self, &proc)
end
parse(inp) click to toggle source
# File lib/functional_parser.rb, line 18
def parse(inp)
  @f.call(inp)
end
|(other) click to toggle source
# File lib/functional_parser.rb, line 26
def |(other)
  FunctionalParser.either(self, other)
end