Class: Reader
- Inherits:
-
Object
- Object
- Reader
- Defined in:
- lib/nendo.rb
Constant Summary collapse
- T_EOF =
tokens
:t_eof- T_LPAREN =
:t_lparen- T_RPAREN =
:t_rparen- T_SYMBOL =
:t_symbol- 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
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 …
- #readwhile(exp) ⇒ Object
- #reset ⇒ Object
-
#sexp ⇒ Object
sexp := ( list ) | ‘sexp | `sexp | atom.
- #skipEnter ⇒ Object
- #skipspace ⇒ Object
- #sourcefile ⇒ Object
- #token ⇒ Object
- #tokenWithComment ⇒ Object
Constructor Details
#initialize(inport, sourcefile, debug = false) ⇒ Reader
inport is IO class
197 198 199 200 201 |
# File 'lib/nendo.rb', line 197 def initialize( inport, sourcefile, debug = false ) @chReader = CharReader.new( inport, sourcefile ) @curtoken = nil @debug = debug end |
Instance Method Details
#_read ⇒ Object
return value is [ S-expression-tree, eof-flag, valid-sexp-flag ]
463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/nendo.rb', line 463 def _read 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
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/nendo.rb', line 329 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 sym.setLispToken( cur ) case sym when :true true when :false false when :nil nil else sym 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_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 else raise "Error: Unknown token in atom()" end end |
#curtoken ⇒ Object
322 323 324 325 326 327 |
# File 'lib/nendo.rb', line 322 def curtoken if !@curtoken self.token end @curtoken end |
#lineno ⇒ Object
211 212 213 |
# File 'lib/nendo.rb', line 211 def lineno @chReader.lineno end |
#list ⇒ Object
list := sexp
| atom ... atom
| atom ... . atom
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 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/nendo.rb', line 375 def list printf( " NonT: [%s]\n", "list" ) if @debug dotted = false cells = [] lastAtom = nil while true case curtoken.kind when T_LINEFEED token # skipEnter when T_EOF raise SyntaxError, "Error: unbalanced paren(1)" when T_LPAREN 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 ... ) form token lastAtom = sexp() end when T_QUOTE , T_QUASIQUOTE , T_UNQUOTE , T_UNQUOTE_SPLICING cells << Cell.new( sexp() ) else if lastAtom 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 if lastAtom cells.first.cdr = lastAtom end cells.first elsif 1 < cells.size ptr = cells.pop if lastAtom ptr.cdr = lastAtom end cells.reverse.each { |x| x.cdr = ptr ptr = x } cells.first end end |
#readwhile(exp) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/nendo.rb', line 224 def readwhile( exp ) 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 end ret end |
#reset ⇒ Object
203 204 205 |
# File 'lib/nendo.rb', line 203 def reset @chReader.reset end |
#sexp ⇒ Object
sexp := ( list ) | ‘sexp | `sexp | atom
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/nendo.rb', line 437 def sexp printf( " NonT: [%s]\n", "sexp" ) if @debug case curtoken.kind when T_LINEFEED token sexp() when T_EOF raise SyntaxError, "Error: unbalanced paren(2)" when T_LPAREN skipEnter token # consume '(' ret = list() skipEnter token # consume ')' ret when T_RPAREN raise SyntaxError, "Error: unbalanced paren(3)" when T_QUOTE , T_QUASIQUOTE , T_UNQUOTE , T_UNQUOTE_SPLICING _atom = atom() ## "quote" symbol Cell.new( _atom, Cell.new( sexp() )) else atom() end end |
#skipEnter ⇒ Object
430 431 432 433 434 |
# File 'lib/nendo.rb', line 430 def skipEnter while T_LINEFEED == curtoken.kind token end end |
#skipspace ⇒ Object
215 216 217 218 219 220 221 222 |
# File 'lib/nendo.rb', line 215 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
207 208 209 |
# File 'lib/nendo.rb', line 207 def sourcefile @chReader.sourcefile end |
#token ⇒ Object
315 316 317 318 319 320 |
# File 'lib/nendo.rb', line 315 def token begin tokenWithComment end while T_COMMENT == curtoken.kind curtoken end |
#tokenWithComment ⇒ Object
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 |
# File 'lib/nendo.rb', line 242 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( /[@]/ ) if 1 == str.length T_UNQUOTE else T_UNQUOTE_SPLICING end when '(' T_LPAREN when ')' T_RPAREN when '.' str += readwhile( /[_a-zA-Z0-9!?.-]/ ).gsub( /[-]/, '_' ) if 1 == str.length T_DOT else T_SYMBOL end when /[\r\n]/ T_LINEFEED when /;/ readwhile( /[^\r\n]/ ) str = "" T_COMMENT when /[#!]/ readwhile( /[^\r\n]/ ) str = "" T_COMMENT when /[_a-zA-Z]/ # symbol str += readwhile( /[_a-zA-Z0-9!?*.-]/ ).gsub( /[-]/, '_' ) T_SYMBOL when /[*\/=!<>&|%]/ # symbol str += readwhile( /[+*\/=!<>&|?%-]/ ) if str.match( /^[=][>]$/ ) T_FEEDTO else T_SYMBOL end when /[+-]/ # number str += readwhile( /[0-9.]/ ) if 1 < str.length T_NUM else T_SYMBOL end when /[0-9]/ # number str += readwhile( /[0-9.]/ ) T_NUM when /["]/ # String str = LispString.new( readwhile( /[^"]/ )) ; readwhile( /["]/ ) T_STRING else str += readwhile( /[^ \t\r\n]/ ) raise NameError, sprintf( "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 |