source.mx raw

   1  package main
   2  
   3  import (
   4  	"io"
   5  	"unicode/utf8"
   6  )
   7  
   8  type Source struct {
   9  	in   io.Reader
  10  	errh func(line, col uint32, msg string)
  11  
  12  	buf       []byte
  13  	ioerr     error
  14  	b, r, e   int32
  15  	line, col uint32
  16  	ch        rune
  17  	chw       int32
  18  }
  19  
  20  const sentinel = 0x80
  21  
  22  func (s *Source) release() {
  23  	s.buf = nil
  24  	s.in = nil
  25  	s.errh = nil
  26  	s.ioerr = nil
  27  }
  28  
  29  func (s *Source) init(in io.Reader, errh func(line, col uint32, msg string)) {
  30  	s.in = in
  31  	s.errh = errh
  32  
  33  	if s.buf == nil {
  34  		s.buf = []byte{:nextSize(0)}
  35  	}
  36  	s.buf[0] = sentinel
  37  	s.ioerr = nil
  38  	s.b, s.r, s.e = -1, 0, 0
  39  	s.line, s.col = 0, 0
  40  	s.ch = ' '
  41  	s.chw = 0
  42  }
  43  
  44  const Linebase = 1
  45  const Colbase = 1
  46  
  47  func (s *Source) pos() (line, col uint32) {
  48  	return Linebase + s.line, Colbase + s.col
  49  }
  50  
  51  func (s *Source) Debugpos() (line, col uint32) {
  52  	return Linebase + s.line, Colbase + s.col
  53  }
  54  
  55  func (s *Source) error(msg string) {
  56  	line, col := s.pos()
  57  	s.errh(line, col, msg)
  58  }
  59  
  60  func (s *Source) start()          { s.b = s.r - s.chw }
  61  func (s *Source) stop()           { s.b = -1 }
  62  func (s *Source) segment() []byte { return s.buf[s.b : s.r-s.chw] }
  63  
  64  func (s *Source) rewind() {
  65  	if s.b < 0 {
  66  		panic("no active segment")
  67  	}
  68  	s.col -= uint32(s.r - s.b)
  69  	s.r = s.b
  70  	s.nextch()
  71  }
  72  
  73  func (s *Source) nextch() {
  74  redo:
  75  	s.col += uint32(s.chw)
  76  	if s.ch == '\n' {
  77  		s.line++
  78  		s.col = 0
  79  	}
  80  
  81  	if s.ch = rune(s.buf[s.r]); s.ch < sentinel {
  82  		s.r++
  83  		s.chw = 1
  84  		if s.ch == 0 {
  85  			s.error("invalid NUL character")
  86  			goto redo
  87  		}
  88  		return
  89  	}
  90  
  91  	for s.e-s.r < utf8.UTFMax && !utf8.FullRune(s.buf[s.r:s.e]) && s.ioerr == nil {
  92  		s.fill()
  93  	}
  94  
  95  	if s.r == s.e {
  96  		if s.ioerr != io.EOF {
  97  			s.error("I/O error: " | s.ioerr.Error())
  98  			s.ioerr = nil
  99  		}
 100  		s.ch = -1
 101  		s.chw = 0
 102  		return
 103  	}
 104  
 105  	var w int32
 106  	s.ch, w = utf8.DecodeRune(s.buf[s.r:s.e])
 107  	s.chw = int32(w)
 108  	s.r += s.chw
 109  
 110  	if s.ch == utf8.RuneError && s.chw == 1 {
 111  		s.error("invalid UTF-8 encoding")
 112  		goto redo
 113  	}
 114  
 115  	const BOM = 0xfeff
 116  	if s.ch == BOM {
 117  		if s.line > 0 || s.col > 0 {
 118  			s.error("invalid BOM in the middle of the file")
 119  		}
 120  		goto redo
 121  	}
 122  }
 123  
 124  func (s *Source) fill() {
 125  	b := s.r
 126  	if s.b >= 0 {
 127  		b = s.b
 128  		s.b = 0
 129  	}
 130  	content := s.buf[b:s.e]
 131  
 132  	if len(content)*2 > len(s.buf) {
 133  		s.buf = []byte{:nextSize(int32(len(s.buf)))}
 134  		copy(s.buf, content)
 135  	} else if b > 0 {
 136  		copy(s.buf, content)
 137  	}
 138  	s.r -= b
 139  	s.e -= b
 140  
 141  	for i := 0; i < 10; i++ {
 142  		var n int32
 143  		var nn int32
 144  		nn, s.ioerr = s.in.Read(s.buf[s.e : len(s.buf)-1])
 145  		n = int32(nn)
 146  		if n < 0 {
 147  			panic("negative read")
 148  		}
 149  		if n > 0 || s.ioerr != nil {
 150  			s.e += n
 151  			s.buf[s.e] = sentinel
 152  			return
 153  		}
 154  	}
 155  
 156  	s.buf[s.e] = sentinel
 157  	s.ioerr = io.ErrNoProgress
 158  }
 159  
 160  func nextSize(size int32) int32 {
 161  	const min = 4 << 10
 162  	const max = 1 << 20
 163  	if size < min {
 164  		return min
 165  	}
 166  	if size <= max {
 167  		return size << 1
 168  	}
 169  	return size + max
 170  }
 171