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