class Elparser::Parser

parser class for

Constants

Racc_arg
Racc_debug_parser
Racc_token_to_s_table
UNESCAPES
UNESCAPES_REGEX

Public Instance Methods

_reduce_none(val, _values, result) click to toggle source
# File lib/elparser/parser.tab.rb, line 236
def _reduce_none(val, _values, result)
  val[0]
end
_unescape_string(str) click to toggle source
# File lib/elparser.rb, line 278
def _unescape_string(str)
  return str.gsub(UNESCAPES_REGEX) do
    if $1
      if $1 == '\\' then '\\' else UNESCAPES[$1] end
    elsif $2 # escape \u0000 unicode
      ["#$2".hex].pack('U*')
    elsif $3 # escape \u{0000} unicode
      ["#$3".hex].pack('U*')
    elsif $4 # escape \0xff or \xff
      [$4].pack('H2')
    end
  end
end
next_token() click to toggle source
# File lib/elparser.rb, line 292
def next_token
  @tokens.shift
end
normalize(ast) click to toggle source

replace special symbols

# File lib/elparser.rb, line 297
def normalize(ast)
  if ast.class == SExpSymbol
    case ast.name
    when "nil"
      return SExpNil.new
    else
      return ast
    end
  elsif ast.cons? then
    ast.visit do |i|
      normalize(i)
    end
  end
  return ast
end
parse(str) click to toggle source

parse s-expression string and return sexp objects.

# File lib/elparser.rb, line 245
def parse(str)
  if str.nil? || str == ""
    raise ParserError.new("Empty input",0,"")
  end

  s = StringScanner.new str
  @tokens = []

  until s.eos?
    s.skip(/\s+/) ? nil :
      s.scan(/\A[-+]?[0-9]*\.[0-9]+(e[-+]?[0-9]+)?/i) ? (@tokens << [:FLOAT, s.matched]) :
      s.scan(/\A[-+]?(0|[1-9]\d*)/)      ? (@tokens << [:INTEGER, s.matched]) :
      s.scan(/\A\.(?=\s)/)               ? (@tokens << ['.', '.']) :
      s.scan(/\A[a-z\-.\/_:*<>+=$#][a-z\-.\/_:$*<>+=0-9]*/i) ? (@tokens << [:SYMBOL, s.matched]) :
      s.scan(/\A"(([^\\"]|\\.)*)"/)        ? (@tokens << [:STRING, _unescape_string(s.matched.slice(1...-1))])  :
      s.scan(/\A./)                      ? (a = s.matched; @tokens << [a, a]) :
      (raise ParserError.new("Scanner error",s.pos,s.peek(5)))
  end
  @tokens.push [false, 'END']

  return do_parse.map do |i|
    normalize(i)
  end
end
parse1(str) click to toggle source

parse s-expression string and return one sexp object.

# File lib/elparser.rb, line 240
def parse1(str)
  parse(str)[0]
end