Class: Cyc::Parser
- Inherits:
-
Object
- Object
- Cyc::Parser
- Defined in:
- lib/cyc/parser.rb
Overview
- Author
-
Aleksander Pohl ([email protected])
- License
-
MIT/X11 License
The class used to parse the answer of the Cyc server.
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
-
#parse(message, stack = nil) ⇒ Object
Parses message received from server.
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
7 8 9 |
# File 'lib/cyc/parser.rb', line 7 def initialize @lexer = SExpressionLexer.new end |
Instance Method Details
#parse(message, stack = nil) ⇒ Object
Parses message received from server. Accepts message
to parse and a stack
with a partial parse result.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cyc/parser.rb', line 13 def parse(,stack=nil) @lexer.scan_str() stack ||= [[]] while !(token = @lexer.next_token).nil? #p token case token[0] when :open_par stack.push [] when :close_par top = stack.pop stack[-1].push top when :atom if token[1] == "T" stack[-1] << true else # FIXME find way to differentiate strings and atoms stack[-1] << token[1] end when :cyc_symbol stack[-1] << ::Cyc::Symbol.new(token[1][1..-1]) when :variable stack[-1] << ::Cyc::Variable.new(token[1][1..-1]) when :term stack[-1] << token[1][2..-1].to_sym when :string stack[-1] << token[1] when :open_as stack.push [:as] when :close_quote top = stack.pop if top[0] == :as as = Assertion.new(top[1],top[2]) stack[-1].push as else stack.push top end when :continuation top = stack.pop stack[-1].push top raise ContinueParsing.new(stack[0][0]) when :nil stack[-1] << nil when :assertion_sep # ignore end end stack[0][0] rescue ContinueParsing => ex raise rescue Exception => ex raise ParserError.new("Exception #{ex} occurred when parsing message '#{}'.") end |