Class: Nendo::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/nendo.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_REGEXP =
:t_regexp

Instance Method Summary collapse

Constructor Details

#initialize(inport, sourcefile, debug = false) ⇒ Reader

inport is IO class



336
337
338
339
340
341
342
# File 'lib/nendo.rb', line 336

def initialize( inport, sourcefile, debug = false )
  @inport     = inport
  @sourcefile = sourcefile
  @chReader   = nil
  @curtoken   = nil
  @debug      = debug
end

Instance Method Details

#_readObject

return value is [ S-expression-tree, eof-flag, valid-sexp-flag ]



798
799
800
801
802
803
804
805
806
807
808
809
# File 'lib/nendo.rb', line 798

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

#atomObject



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/nendo.rb', line 599

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_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_KEYWORD
    LispKeyword.new( cur.str )
  else
    raise "Error: Unknown token in atom()"
  end
end

#curtokenObject



592
593
594
595
596
597
# File 'lib/nendo.rb', line 592

def curtoken
  if !@curtoken
    self.token
  end
  @curtoken
end

#linenoObject



352
353
354
355
356
357
358
# File 'lib/nendo.rb', line 352

def lineno
  if @chReader
    @chReader.lineno
  else
    1
  end
end

#listObject

list := sexp

| atom ... atom
| atom ... . atom


682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/nendo.rb', line 682

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



388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/nendo.rb', line 388

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

#readRegexpObject



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/nendo.rb', line 436

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

#readstringObject



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
429
430
431
432
433
434
# File 'lib/nendo.rb', line 402

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



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/nendo.rb', line 369

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

#resetObject



344
345
346
# File 'lib/nendo.rb', line 344

def reset
  @chReader.reset  if @chReader
end

#sexpObject

sexp := ( list ) | | #( vector ) | ‘sexp | `sexp | atom



748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/nendo.rb', line 748

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

#skipEnterObject



741
742
743
744
745
# File 'lib/nendo.rb', line 741

def skipEnter
  while T_LINEFEED == curtoken.kind
    token
  end
end

#skipspaceObject



360
361
362
363
364
365
366
367
# File 'lib/nendo.rb', line 360

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

#sourcefileObject



348
349
350
# File 'lib/nendo.rb', line 348

def sourcefile
  @sourcefile
end

#tokenObject



585
586
587
588
589
590
# File 'lib/nendo.rb', line 585

def token
  begin
    tokenWithComment
  end while T_COMMENT == curtoken.kind
  curtoken
end

#tokenWithCommentObject



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
512
513
514
515
516
517
518
519
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/nendo.rb', line 457

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
          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

#vectorObject

vector := sexp

| atom ... atom


650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/nendo.rb', line 650

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