class Satre::Parser

Public Class Methods

is_const_name(string) click to toggle source
# File lib/satre/parser/parser.rb, line 14
def is_const_name(string)
  string.split('').all? { |c| Lexer.numeric?(c)  } || string.nil?
end
make_parser(parsing_function, input) click to toggle source
# File lib/satre/parser/parser.rb, line 8
def make_parser(parsing_function, input)
  expr, rest = parsing_function.call(Lexer.lex(input))
  fail(ParserError, "Unparsed input: #{rest}") unless rest == []
  return expr
end
nextin(inp, tok) click to toggle source

Checks if the head of a list (typically the list of unparsed input) is some particular item, but also first checks that the list is nonempty before looking at its head

# File lib/satre/parser/parser.rb, line 65
def nextin(inp, tok)
  inp != [] && inp.first == tok
end
papply(f, ast, rest) click to toggle source

Applies a function to the first element of a pair. The idea being to modify the returned abstract syntax tree while leaving the ‘unparsed input` alone

# File lib/satre/parser/parser.rb, line 58
def papply(f, ast, rest)
  return f.call(ast), rest
end
parse_bracketed(subparser, closing_character, inp) click to toggle source

Deals with the common situation of syntastic items enclosed in brackets It simply calls the subparser and then checks and eliminates the closing bracket. In principle, the therminating character can be anything, so this function could equally be used for other purposes.

# File lib/satre/parser/parser.rb, line 73
def parse_bracketed(subparser, closing_character, inp)
  ast, rest = subparser.call(inp)
  return ast, rest[1..-1] if nextin(rest, closing_character)
  fail(ExpressionError, "Expected closing character '#{closing_character}'")
end
parse_ginfix(opsym, opupdate, sof, subparser, inp) click to toggle source

Generic parsing function sof: function -> takes the current input and combines it in some way with the items arravied at so far opupdate: function -> modifies the function appropriately when an new item is parsed.

# File lib/satre/parser/parser.rb, line 23
def parse_ginfix(opsym, opupdate, sof, subparser, inp)
  e1, inp1 = subparser.call(inp)
  if inp1 != [] && inp1[0] == opsym
    parse_ginfix(opsym, opupdate, opupdate.curry.call(sof, e1), subparser, inp1[1..-1])
  else
    return sof.call(e1), inp1
  end
end
parse_left_infix(opsym, opcon) click to toggle source

parse a list of items and combine them in a left associated manner returns a lambda

# File lib/satre/parser/parser.rb, line 34
def parse_left_infix(opsym, opcon)
  opupdate = ->(f, e1 ,e2) {opcon.call(f.call(e1), e2) }
  sof = ->(x) { x }
  method(:parse_ginfix).curry.call(opsym, opupdate, sof)
end
parse_list(opsym) click to toggle source

parse a list of items and collect them in a list

# File lib/satre/parser/parser.rb, line 49
def parse_list(opsym)
  opupdate = ->(f,e1,e2) {  f.call(e1) << e2 }
  sof = ->(x) { [x] }
  method(:parse_ginfix).curry.call(opsym, opupdate, sof)
end
parse_right_infix(opsym, opcon) click to toggle source

parse a list of items and combine them in a right associated manner returns a lambda

# File lib/satre/parser/parser.rb, line 42
def parse_right_infix(opsym, opcon)
  opupdate = ->(f,e1,e2) { f.call(opcon.call(e1,e2)) }
  sof = ->(x) { x }
  method(:parse_ginfix).curry.call(opsym, opupdate, sof)
end