Class: Cyc::SExpressionLexer

Inherits:
Object
  • Object
show all
Defined in:
lib/cyc/sexpr.rex.rb

Defined Under Namespace

Classes: ScanError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename



14
15
16
# File 'lib/cyc/sexpr.rex.rb', line 14

def filename
  @filename
end

#linenoObject (readonly)

Returns the value of attribute lineno



13
14
15
# File 'lib/cyc/sexpr.rex.rb', line 13

def lineno
  @lineno
end

Instance Method Details

#action(&block) ⇒ Object



18
19
20
# File 'lib/cyc/sexpr.rex.rb', line 18

def action &block
  yield
end

#do_parseObject

def scan_evaluate



139
140
# File 'lib/cyc/sexpr.rex.rb', line 139

def do_parse
end

#load_file(filename) ⇒ Object



27
28
29
30
31
32
# File 'lib/cyc/sexpr.rex.rb', line 27

def load_file( filename )
  @filename = filename
  open(filename, "r") do |f|
    scan_evaluate  f.read
  end
end

#next_tokenObject



39
40
41
# File 'lib/cyc/sexpr.rex.rb', line 39

def next_token
  @rex_tokens.shift
end

#scan_evaluate(str) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cyc/sexpr.rex.rb', line 43

def scan_evaluate( str )
  scan_setup
  @rex_tokens = []
  @lineno  =  1
  ss = StringScanner.new(str)
  state = nil
  until ss.eos?
    text = ss.peek(1)
    @lineno  +=  1  if text == "\n"
    case state
    when nil
      case
      when (text = ss.scan(/\#<AS:/))
         @rex_tokens.push action { [:open_as,text] }

      when (text = ss.scan(/\#</))
         @rex_tokens.push action { [:open_quote,text] }

      when (text = ss.scan(/>/))
         @rex_tokens.push action { [:close_quote,text] }

      when (text = ss.scan(/\.\.\./))
         @rex_tokens.push action { [:continuation] }

      when (text = ss.scan(/\(/))
         @rex_tokens.push action { [:open_par,text] }

      when (text = ss.scan(/\)/))
         @rex_tokens.push action { [:close_par,text] }

      when (text = ss.scan(/NIL/))
         @rex_tokens.push action { [:nil,text] }

      when (text = ss.scan(/:[^<>\r\n\"\(\):&\?\#\ ]+/))
         @rex_tokens.push action { [:cyc_symbol,text] }

      when (text = ss.scan(/\?[^<>\r\n\"\(\):&\?\#\ ]+/))
         @rex_tokens.push action { [:variable,text] }

      when (text = ss.scan(/\#\$[a-zA-Z0-9:_-]+/))
         @rex_tokens.push action { [:term,text] }

      when (text = ss.scan(/[^\r\n\"\(\):&\ ]+/))
         @rex_tokens.push action { [:atom,text] }

      when (text = ss.scan(/\"/))
         @rex_tokens.push action { state = :STRING; @str = ""; [:in_string] }

      when (text = ss.scan(/:/))
         @rex_tokens.push action { [:assertion_sep]}

      when (text = ss.scan(/[\ \t\f\r\n]/))
        ;

      when (text = ss.scan(/.|\n/))
         @rex_tokens.push action { raise "Illegal character <#{text}>" }

      else
        text = ss.string[ss.pos .. -1]
        raise  ScanError, "can not match: '" + text + "'"
      end  # if

    when :STRING
      case
      when (text = ss.scan(/[^\n\r\"\\]+/))
         @rex_tokens.push action { @str << text; [:in_string]}

      when (text = ss.scan(/\t/))
         @rex_tokens.push action { @str << "\t"; [:in_string] }

      when (text = ss.scan(/\n/))
         @rex_tokens.push action { @str << "\n"; [:in_string] }

      when (text = ss.scan(/\r/))
         @rex_tokens.push action { @str << "\n"; [:in_string] }

      when (text = ss.scan(/\\"/))
         @rex_tokens.push action { @str << '"'; [:in_string] }

      when (text = ss.scan(/\\/))
         @rex_tokens.push action { @str << "\\"; [:in_string] }

      when (text = ss.scan(/\"/))
         @rex_tokens.push action { state = nil; [:string,@str] }

      else
        text = ss.string[ss.pos .. -1]
        raise  ScanError, "can not match: '" + text + "'"
      end  # if

    else
      raise  ScanError, "undefined state: '" + state.to_s + "'"
    end  # case state
  end  # until ss
end

#scan_file(filename) ⇒ Object



34
35
36
37
# File 'lib/cyc/sexpr.rex.rb', line 34

def scan_file( filename )
  load_file  filename
  do_parse
end

#scan_setupObject



16
# File 'lib/cyc/sexpr.rex.rb', line 16

def scan_setup ; end

#scan_str(str) ⇒ Object



22
23
24
25
# File 'lib/cyc/sexpr.rex.rb', line 22

def scan_str( str )
  scan_evaluate  str
  do_parse
end