github.com/gopherd/gonum@v0.0.4/graph/formats/dot/internal/lexer/lexer.go (about)

     1  // Code generated by gocc; DO NOT EDIT.
     2  
     3  // This file is dual licensed under CC0 and The Gonum License.
     4  //
     5  // Copyright ©2017 The Gonum Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  //
     9  // Copyright ©2017 Robin Eklind.
    10  // This file is made available under a Creative Commons CC0 1.0
    11  // Universal Public Domain Dedication.
    12  
    13  package lexer
    14  
    15  import (
    16  	"io/ioutil"
    17  	"unicode/utf8"
    18  
    19  	"github.com/gopherd/gonum/graph/formats/dot/internal/token"
    20  )
    21  
    22  const (
    23  	NoState    = -1
    24  	NumStates  = 143
    25  	NumSymbols = 184
    26  )
    27  
    28  type Lexer struct {
    29  	src    []byte
    30  	pos    int
    31  	line   int
    32  	column int
    33  }
    34  
    35  func NewLexer(src []byte) *Lexer {
    36  	lexer := &Lexer{
    37  		src:    src,
    38  		pos:    0,
    39  		line:   1,
    40  		column: 1,
    41  	}
    42  	return lexer
    43  }
    44  
    45  func NewLexerFile(fpath string) (*Lexer, error) {
    46  	src, err := ioutil.ReadFile(fpath)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return NewLexer(src), nil
    51  }
    52  
    53  func (l *Lexer) Scan() (tok *token.Token) {
    54  	tok = new(token.Token)
    55  	if l.pos >= len(l.src) {
    56  		tok.Type = token.EOF
    57  		tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = l.pos, l.line, l.column
    58  		return
    59  	}
    60  	start, startLine, startColumn, end := l.pos, l.line, l.column, 0
    61  	tok.Type = token.INVALID
    62  	state, rune1, size := 0, rune(-1), 0
    63  	for state != -1 {
    64  		if l.pos >= len(l.src) {
    65  			rune1 = -1
    66  		} else {
    67  			rune1, size = utf8.DecodeRune(l.src[l.pos:])
    68  			l.pos += size
    69  		}
    70  
    71  		nextState := -1
    72  		if rune1 != -1 {
    73  			nextState = TransTab[state](rune1)
    74  		}
    75  		state = nextState
    76  
    77  		if state != -1 {
    78  
    79  			switch rune1 {
    80  			case '\n':
    81  				l.line++
    82  				l.column = 1
    83  			case '\r':
    84  				l.column = 1
    85  			case '\t':
    86  				l.column += 4
    87  			default:
    88  				l.column++
    89  			}
    90  
    91  			switch {
    92  			case ActTab[state].Accept != -1:
    93  				tok.Type = ActTab[state].Accept
    94  				end = l.pos
    95  			case ActTab[state].Ignore != "":
    96  				start, startLine, startColumn = l.pos, l.line, l.column
    97  				state = 0
    98  				if start >= len(l.src) {
    99  					tok.Type = token.EOF
   100  				}
   101  
   102  			}
   103  		} else {
   104  			if tok.Type == token.INVALID {
   105  				end = l.pos
   106  			}
   107  		}
   108  	}
   109  	if end > start {
   110  		l.pos = end
   111  		tok.Lit = l.src[start:end]
   112  	} else {
   113  		tok.Lit = []byte{}
   114  	}
   115  	tok.Pos.Offset, tok.Pos.Line, tok.Pos.Column = start, startLine, startColumn
   116  
   117  	return
   118  }
   119  
   120  func (l *Lexer) Reset() {
   121  	l.pos = 0
   122  }
   123  
   124  /*
   125  Lexer symbols:
   126  0: 'n'
   127  1: 'o'
   128  2: 'd'
   129  3: 'e'
   130  4: 'N'
   131  5: 'o'
   132  6: 'd'
   133  7: 'e'
   134  8: 'N'
   135  9: 'O'
   136  10: 'D'
   137  11: 'E'
   138  12: 'e'
   139  13: 'd'
   140  14: 'g'
   141  15: 'e'
   142  16: 'E'
   143  17: 'd'
   144  18: 'g'
   145  19: 'e'
   146  20: 'E'
   147  21: 'D'
   148  22: 'G'
   149  23: 'E'
   150  24: 'g'
   151  25: 'r'
   152  26: 'a'
   153  27: 'p'
   154  28: 'h'
   155  29: 'G'
   156  30: 'r'
   157  31: 'a'
   158  32: 'p'
   159  33: 'h'
   160  34: 'G'
   161  35: 'R'
   162  36: 'A'
   163  37: 'P'
   164  38: 'H'
   165  39: 'd'
   166  40: 'i'
   167  41: 'g'
   168  42: 'r'
   169  43: 'a'
   170  44: 'p'
   171  45: 'h'
   172  46: 'D'
   173  47: 'i'
   174  48: 'g'
   175  49: 'r'
   176  50: 'a'
   177  51: 'p'
   178  52: 'h'
   179  53: 'd'
   180  54: 'i'
   181  55: 'G'
   182  56: 'r'
   183  57: 'a'
   184  58: 'p'
   185  59: 'h'
   186  60: 'D'
   187  61: 'i'
   188  62: 'G'
   189  63: 'r'
   190  64: 'a'
   191  65: 'p'
   192  66: 'h'
   193  67: 'D'
   194  68: 'I'
   195  69: 'G'
   196  70: 'R'
   197  71: 'A'
   198  72: 'P'
   199  73: 'H'
   200  74: 's'
   201  75: 'u'
   202  76: 'b'
   203  77: 'g'
   204  78: 'r'
   205  79: 'a'
   206  80: 'p'
   207  81: 'h'
   208  82: 'S'
   209  83: 'u'
   210  84: 'b'
   211  85: 'g'
   212  86: 'r'
   213  87: 'a'
   214  88: 'p'
   215  89: 'h'
   216  90: 's'
   217  91: 'u'
   218  92: 'b'
   219  93: 'G'
   220  94: 'r'
   221  95: 'a'
   222  96: 'p'
   223  97: 'h'
   224  98: 'S'
   225  99: 'u'
   226  100: 'b'
   227  101: 'G'
   228  102: 'r'
   229  103: 'a'
   230  104: 'p'
   231  105: 'h'
   232  106: 'S'
   233  107: 'U'
   234  108: 'B'
   235  109: 'G'
   236  110: 'R'
   237  111: 'A'
   238  112: 'P'
   239  113: 'H'
   240  114: 's'
   241  115: 't'
   242  116: 'r'
   243  117: 'i'
   244  118: 'c'
   245  119: 't'
   246  120: 'S'
   247  121: 't'
   248  122: 'r'
   249  123: 'i'
   250  124: 'c'
   251  125: 't'
   252  126: 'S'
   253  127: 'T'
   254  128: 'R'
   255  129: 'I'
   256  130: 'C'
   257  131: 'T'
   258  132: '{'
   259  133: '}'
   260  134: ';'
   261  135: '-'
   262  136: '-'
   263  137: '-'
   264  138: '>'
   265  139: '['
   266  140: ']'
   267  141: ','
   268  142: '='
   269  143: ':'
   270  144: '_'
   271  145: '-'
   272  146: '.'
   273  147: '-'
   274  148: '.'
   275  149: '\'
   276  150: '"'
   277  151: '\'
   278  152: '"'
   279  153: '"'
   280  154: '='
   281  155: '<'
   282  156: '>'
   283  157: '<'
   284  158: '>'
   285  159: '/'
   286  160: '/'
   287  161: '\n'
   288  162: '#'
   289  163: '\n'
   290  164: '/'
   291  165: '*'
   292  166: '*'
   293  167: '*'
   294  168: '/'
   295  169: ' '
   296  170: '\t'
   297  171: '\r'
   298  172: '\n'
   299  173: \u0001-'!'
   300  174: '#'-'['
   301  175: ']'-\u007f
   302  176: 'a'-'z'
   303  177: 'A'-'Z'
   304  178: '0'-'9'
   305  179: \u0080-\ufffc
   306  180: \ufffe-\U0010ffff
   307  181: \u0001-';'
   308  182: '?'-\u00ff
   309  183: .
   310  */