Class: TlaParserS::Parser

Inherits:
Object
  • Object
show all
Includes:
Utils::MyLogger
Defined in:
lib/parser/parser.rb

Constant Summary collapse

PROGNAME =

Logger

"parser"
@@parser =

Load the Treetop grammar from the ‘sexp_parser’ file, and create a new instance of that parser as a class variable so we don’t have to re-create it every time we need to parse a string

nil

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger, #logfile

Constructor Details

#initialize(options = {}) ⇒ Parser


constructore



41
42
43
44
45
46
# File 'lib/parser/parser.rb', line 41

def initialize( options = {} )
  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__} initialized" )
  
  self.loadParsers
end

Class Method Details

.versionObject



33
34
35
# File 'lib/parser/parser.rb', line 33

def self.version
  TlaParserS::version
end

Instance Method Details

#getTheParserSexpParser

Returns static parser to use.

Returns:

  • (SexpParser)

    static parser to use



106
107
108
# File 'lib/parser/parser.rb', line 106

def getTheParser
  @@parser
end

#loadParsersSexpParser

Returns cached parser from ‘parser_sexp.treetop’.

Returns:

  • (SexpParser)

    cached parser from ‘parser_sexp.treetop’



49
50
51
52
53
54
55
56
57
58
# File 'lib/parser/parser.rb', line 49

def loadParsers
  # cached
  return @@parser  unless @@parser.nil?
  base_path   = File.expand_path(File.dirname(__FILE__))
  parser_path = File.join( base_path, 'parser_sexp.treetop')
  @logger.info( "#{__method__} loading parser from parser path #{parser_path}" )
  Treetop.load( parser_path)
  @@parser = SbuilderSexpParser.new
  @@parser 
end

#parse(data, start = nil) ⇒ Object

Parameters:

  • start (symbol) (defaults to: nil)

    treep grammar non-terminal to start iwth



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/parser/parser.rb', line 67

def parse( data, start = nil )
  return nil if data.nil?

  
  # to pass a file
  if data.respond_to? :read
    data = data.read
  # elsif ! data.respond_to? :lines
  #   data.lines = data.to_a
  end

  
  # Pass the data over to the parser instance
  parser = getTheParser
  tree = parser.parse(data, :root=>start )
  
  # If the AST is nil then there was an error during parsing
  # we need to report a simple error message to help the user
  if(tree.nil?)
    # adopted from http://whitequark.org/blog/2011/09/08/treetop-typical-errors/
    parser.failure_reason =~ /(Expected .*) after/m
    expected = $1.nil? ? parser.failure_reason : $1
    msg= <<-EOS
    Parse error:
    Line     : #{parser.failure_line}, offset   : #{parser.index}
    #{expected.gsub( "\n", '$NEWLINE')}
    #{data.lines.to_a[ parser.failure_line()-1 ]}
    #{'~' * (parser.failure_column-1)}^
    EOS
    # puts msg
    @logger.error( "#{__method__} #{msg}, for data #{data}" )
    raise ParseException.new msg
  end

  self.class.clean_tree( tree )
  return tree
end