Class: Nendo::Reader
- Inherits:
-
Object
- Object
- Nendo::Reader
- Defined in:
- lib/nendo/ruby/reader.rb
Constant Summary collapse
- T_EOF =
tokens
:t_eof
- T_LPAREN =
:t_lparen
- T_RPAREN =
:t_rparen
- T_LVECTOR =
:t_lvector
- T_SYMBOL =
:t_symbol
- T_KEYWORD =
:t_keyword
- T_NUM =
:t_num
- T_STRING =
:t_string
- T_QUOTE =
:t_quote
- T_QUASIQUOTE =
:t_quasiquote
- T_UNQUOTE =
:t_unquote
- T_UNQUOTE_SPLICING =
:t_unquote_splicing
- T_FEEDTO =
:t_feedto
- T_DOT =
:t_dot
- T_LINEFEED =
:t_linefeed
- T_COMMENT =
:t_comment
- T_DEBUG_PRINT =
:t_debug_print
- T_MACRO_DEBUG_PRINT =
:t_macro_debug_print
- T_REGEXP =
:t_regexp
Instance Method Summary collapse
-
#_read ⇒ Object
return value is [ S-expression-tree, eof-flag, valid-sexp-flag ].
- #atom ⇒ Object
- #curtoken ⇒ Object
-
#initialize(inport, sourcefile, debug = false) ⇒ Reader
constructor
inport is IO class.
- #lineno ⇒ Object
-
#list ⇒ Object
list := sexp | atom …
- #peekchar(exp) ⇒ Object
- #readRegexp ⇒ Object
- #readstring ⇒ Object
- #readwhile(exp, oneshot = false) ⇒ Object
- #reset ⇒ Object
-
#sexp ⇒ Object
sexp := ( list ) | | #( vector ) | ‘sexp | `sexp | atom.
- #skipEnter ⇒ Object
- #skipspace ⇒ Object
- #sourcefile ⇒ Object
- #token ⇒ Object
- #tokenWithComment ⇒ Object
-
#vector ⇒ Object
vector := sexp | atom …
Constructor Details
#initialize(inport, sourcefile, debug = false) ⇒ Reader
inport is IO class
104 105 106 107 108 109 110 |
# File 'lib/nendo/ruby/reader.rb', line 104 def initialize( inport, sourcefile, debug = false ) @inport = inport @sourcefile = sourcefile @chReader = nil @curtoken = nil @debug = debug end |
Instance Method Details
#_read ⇒ Object
return value is [ S-expression-tree, eof-flag, valid-sexp-flag ]
570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/nendo/ruby/reader.rb', line 570 def _read @chReader = CharReader.new( @inport, @sourcefile ) unless @chReader case curtoken.kind when T_EOF [ Nil.new, true, false ] when T_LINEFEED token [ Nil.new, false, false ] else [ sexp(), false, true ] end end |
#atom ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/nendo/ruby/reader.rb', line 370 def atom cur = curtoken printf( " NonT: [%s] : [%s]\n", "atom", cur.str ) if @debug token case cur.kind when T_SYMBOL sym = cur.str.intern case sym when :true true when :false false when :nil nil else ParsedSymbol.new( sym, cur.sourcefile, cur.lineno ) end when T_NUM if cur.str.match( /[.]/ ) # floating point cur.str.to_f else cur.str.to_i end when T_STRING cur.str when T_REGEXP LispRegexp.new( cur.str ) when T_QUOTE :quote when T_QUASIQUOTE :quasiquote when T_UNQUOTE :unquote when T_UNQUOTE_SPLICING :"unquote-splicing" when T_DOT :"dot-operator" when T_FEEDTO :feedto when T_DEBUG_PRINT "debug-print".intern when T_MACRO_DEBUG_PRINT LispString.new( sprintf( "%s:%d", cur.sourcefile, cur.lineno )) when T_KEYWORD LispKeyword.new( cur.str ) else raise "Error: Unknown token in atom()" end end |
#curtoken ⇒ Object
363 364 365 366 367 368 |
# File 'lib/nendo/ruby/reader.rb', line 363 def curtoken if !@curtoken self.token end @curtoken end |
#lineno ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/nendo/ruby/reader.rb', line 120 def lineno if @chReader @chReader.lineno else 1 end end |
#list ⇒ Object
list := sexp
| atom ... atom
| atom ... . atom
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/nendo/ruby/reader.rb', line 454 def list printf( " NonT: [%s]\n", "list" ) if @debug dotted = false cells = [] lastAtom = Nil.new while true case curtoken.kind when T_LINEFEED token # skipEnter when T_EOF begin raise RuntimeError, "Error: unbalanced paren(1)" rescue => e e.set_backtrace( [sprintf( "%s:%d", curtoken.sourcefile, curtoken.lineno )] + e.backtrace ) raise e end when T_LPAREN, T_LVECTOR cells << Cell.new( sexp() ) when T_RPAREN break when T_DOT if 0 == cells.length # (. symbol1 symbol2 ... ) form cells << Cell.new( atom() ) else # ( symbol1 ... symbol2 . symbol3 ) form token lastAtom = sexp() if lastAtom.is_a? Cell and lastAtom.isNull lastAtom = Nil.new # the null list "()" could not be a lastAtom. end end when T_QUOTE , T_QUASIQUOTE , T_UNQUOTE , T_UNQUOTE_SPLICING, T_DEBUG_PRINT cells << Cell.new( sexp() ) else if not lastAtom.is_a? Nil raise "Error : illegal dotted pair syntax." else cells << Cell.new( atom() ) end end end ## setup list if 0 == cells.size Cell.new() # null list elsif 1 == cells.size cells.first.cdr = lastAtom cells.first elsif 1 < cells.size ptr = cells.pop ptr.cdr = lastAtom cells.reverse.each { |x| x.cdr = ptr ptr = x } cells.first end end |
#peekchar(exp) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/nendo/ruby/reader.rb', line 156 def peekchar( exp ) ch = @chReader.getc #printf( " peekchar: [%02x]\n", ch ) if @debug if !ch # eof? return nil end if ch.chr.match( exp ) ch.chr else @chReader.ungetc( ch ) nil end end |
#readRegexp ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/nendo/ruby/reader.rb', line 204 def readRegexp() ret = "" while true ch = @chReader.getc #printf( " readRegexp1: [%s]\n", ch ) if !ch # eof? break end if ch.chr == "\\" #escape ch2 = @chReader.getc #printf( " readRegexp2: [%s]\n", ch2 ) ret += "\\" + ch2.chr elsif ch.chr == '/' break else ret += ch.chr end end ret end |
#readstring ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/nendo/ruby/reader.rb', line 170 def readstring() ret = "" while true ch = @chReader.getc #printf( " readstring: [%s]\n", ch ) if !ch # eof? break end if ch.chr == "\\" ch2 = @chReader.getc ret += case ch2.chr when '"' # \" reduce to " '"' when '\\' # \\ reduce to "\\" when 'n' "\n" when 'r' "\r" when 't' "\t" else "" end elsif ch.chr != '"' ret += ch.chr else @chReader.ungetc( ch ) break end end ret end |
#readwhile(exp, oneshot = false) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/nendo/ruby/reader.rb', line 137 def readwhile( exp, oneshot = false ) ret = "" while true ch = @chReader.getc #printf( " readwhile: [%02x]\n", ch ) if @debug if !ch # eof? break end if ch.chr.match( exp ) ret += ch.chr else @chReader.ungetc( ch ) break end if oneshot then break end end ret end |
#reset ⇒ Object
112 113 114 |
# File 'lib/nendo/ruby/reader.rb', line 112 def reset @chReader.reset if @chReader end |
#sexp ⇒ Object
sexp := ( list ) | | #( vector ) | ‘sexp | `sexp | atom
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/nendo/ruby/reader.rb', line 520 def sexp printf( " NonT: [%s]\n", "sexp" ) if @debug case curtoken.kind when T_LINEFEED token sexp() when T_EOF begin raise RuntimeError, "Error: unbalanced paren(2)" rescue => e e.set_backtrace( [sprintf( "%s:%d", curtoken.sourcefile, curtoken.lineno )] + e.backtrace ) raise e end when T_LPAREN skipEnter token # consume '(' ret = list() skipEnter token # consume ')' ret when T_RPAREN token # consume ')' begin raise RuntimeError, "Error: unbalanced vector's paren(3)" rescue => e e.set_backtrace( [sprintf( "%s:%d", curtoken.sourcefile, curtoken.lineno )] + e.backtrace ) raise e end when T_LVECTOR skipEnter token # consume '#(' ret = vector() skipEnter token # consume ')' ret when T_QUOTE , T_QUASIQUOTE , T_UNQUOTE , T_UNQUOTE_SPLICING _atom = atom() ## "quote" symbol Cell.new( _atom, Cell.new( sexp() )) when T_DEBUG_PRINT file = curtoken.sourcefile lineno = curtoken.lineno _atom = atom() ## "debug-print" symbol child = sexp() [_atom, child, LispString.new( file ), lineno, Cell.new( :quote, Cell.new( child )) ].to_list else atom() end end |
#skipEnter ⇒ Object
513 514 515 516 517 |
# File 'lib/nendo/ruby/reader.rb', line 513 def skipEnter while T_LINEFEED == curtoken.kind token end end |
#skipspace ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/nendo/ruby/reader.rb', line 128 def skipspace begin ch = @chReader.getc break if nil == ch # non eof? #printf( " skipspace: [%02x]\n", ch ) if @debug end while ch.chr.match( /[ \t]/ ) @chReader.ungetc( ch ) if nil != ch end |
#sourcefile ⇒ Object
116 117 118 |
# File 'lib/nendo/ruby/reader.rb', line 116 def sourcefile @sourcefile end |
#token ⇒ Object
356 357 358 359 360 361 |
# File 'lib/nendo/ruby/reader.rb', line 356 def token begin tokenWithComment end while T_COMMENT == curtoken.kind curtoken end |
#tokenWithComment ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/nendo/ruby/reader.rb', line 225 def tokenWithComment skipspace ch = @chReader.getc if nil == ch # eof? @curtoken = Token.new( T_EOF, "", @chReader.sourcefile, @chReader.lineno, @chReader.column ) else str = ch.chr kind = case str when /[\']/ T_QUOTE when /[\`]/ T_QUASIQUOTE when /[,]/ str += readwhile( /[@]/, true ) if 1 == str.length T_UNQUOTE else T_UNQUOTE_SPLICING end when '(', '[' T_LPAREN when ')', ']' T_RPAREN when '.' str += readwhile( /[_a-zA-Z0-9!?.]/ ) if 1 == str.length T_DOT else T_SYMBOL end when /[\r\n]/ T_LINEFEED when /;/ readwhile( /[^\r\n]/ ) str = "" T_COMMENT when /[#]/ nextch = peekchar( /[?!tfbodx(\/]/ ) case nextch when "?" if peekchar( /[=]/ ) str = "" T_DEBUG_PRINT elsif peekchar( /[.]/ ) str = "" T_MACRO_DEBUG_PRINT else str += readwhile( /[^ \t\r\n]/ ) raise NameError, sprintf( "Error: unknown #xxxx syntax for Nendo %s", str ) end when "!" readwhile( /[^\r\n]/ ) str = "" T_COMMENT when "(" str = "" T_LVECTOR when "t" str = "true" T_SYMBOL when "f" str = "false" T_SYMBOL when "b","o","d","x" str = readwhile( /[0-9a-zA-Z]/ ) case nextch when "b" if str.match( /^[0-1]+$/ ) str = "0b" + str else raise RuntimeError, sprintf( "Error: illegal #b number for Nendo #b%s", str ) end when "o" if str.match( /^[0-7]+$/ ) str = "0o" + str else raise RuntimeError, sprintf( "Error: illegal #o number for Nendo #o%s", str ) end when "d" if str.match( /^[0-9]+$/ ) str = "0d" + str else raise RuntimeError, sprintf( "Error: illegal #d number for Nendo #d%s", str ) end when "x" if str.match( /^[0-9a-fA-F]+$/ ) str = "0x" + str else raise RuntimeError, sprintf( "Error: illegal #x number for Nendo #x%s", str ) end end str = Integer( str ).to_s T_NUM when "/" # T_REGEXP's str takes "iXXXXX"(igreno case) or " XXXXXX"(case sensitive) value. readwhile( /[\/]/ ) # consume str = readRegexp() str = ((0 < readwhile( /[i]/ ).size) ? "i" : " ") + str T_REGEXP else str += readwhile( /[^ \t\r\n]/ ) raise NameError, sprintf( "Error: unknown #xxxx syntax for Nendo %s", str ) end when /[_a-zA-Z!$%&*+\/:<=>?@^~-]/ # symbol str += readwhile( /[0-9._a-zA-Z!$%&*+\/:<=>?@^~-]/ ) if str.match( /^[=][>]$/ ) T_FEEDTO elsif str.match( /^[+-][0-9.]+$/ ) T_NUM elsif str.match( /^[:]/ ) str = str[1..-1] T_KEYWORD else T_SYMBOL end when /[0-9]/ # Numeric str += readwhile( /[0-9.]/ ) T_NUM when /["]/ # String str = LispString.new( readstring() ) readwhile( /["]/ ) T_STRING else str += readwhile( /[^ \t\r\n]/ ) raise NameError, sprintf( "Error: unknown token for Nendo [%s]", str ) end printf( " token: [%s] : %s (%s:L%d:C%d)\n", str, kind.to_s, @chReader.sourcefile, @chReader.lineno, @chReader.column ) if @debug @curtoken = Token.new( kind, str, @chReader.sourcefile, @chReader.lineno, @chReader.column ) end end |
#vector ⇒ Object
vector := sexp
| atom ... atom
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/nendo/ruby/reader.rb', line 422 def vector printf( " NonT: [%s]\n", "vector" ) if @debug arr = [] while true case curtoken.kind when T_LINEFEED token # skipEnter when T_EOF begin raise RuntimeError, "Error: unbalanced vector's paren(4)" rescue => e e.set_backtrace( [sprintf( "%s:%d", curtoken.sourcefile, curtoken.lineno )] + e.backtrace ) raise e end when T_LPAREN, T_LVECTOR arr << sexp() when T_RPAREN break when T_QUOTE , T_QUASIQUOTE , T_UNQUOTE , T_UNQUOTE_SPLICING, T_DEBUG_PRINT arr << sexp() when T_DOT raise RuntimeError, "Error: illegal list." else arr << atom() end end arr end |