github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/queryparser/classic/tokenManager.go (about)

     1  package classic
     2  
     3  import (
     4  // "fmt"
     5  )
     6  
     7  var jjbitVec0 = []int64{1, 0, 0, 0}
     8  var jjbitVec4 = []uint64{
     9  	0xfffefffffffffffe, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
    10  }
    11  
    12  var jjnextStates = []int{
    13  	37, 39, 40, 17, 18, 20, 42, 45, 31, 46, 43, 22, 23, 25, 26, 24,
    14  	25, 26, 45, 31, 46, 44, 47, 35, 22, 28, 29, 27, 27, 30, 30, 0,
    15  	1, 2, 4, 5,
    16  }
    17  
    18  var jjstrLiteralImages = map[int]string{
    19  	0: "", 11: "\u0053", 12: "\055",
    20  	14: "\050", 15: "\051", 16: "\072", 17: "\052", 18: "\136",
    21  	25: "\133", 26: "\173", 28: "\124\117", 29: "\135", 30: "\175",
    22  }
    23  
    24  var jjnewLexState = []int{
    25  	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1,
    26  	1, 1, 2, -1, 2, 2, -1, -1,
    27  }
    28  
    29  var jjtoToken = []int64{0x1ffffff01}
    30  
    31  type TokenManager struct {
    32  	curLexState     int
    33  	defaultLexState int
    34  	jjnewStateCnt   int
    35  	jjround         int
    36  	jjmatchedPos    int
    37  	jjmatchedKind   int
    38  
    39  	input_stream CharStream
    40  	jjrounds     []int
    41  	jjstateSet   []int
    42  	curChar      rune
    43  }
    44  
    45  func newTokenManager(stream CharStream) *TokenManager {
    46  	return &TokenManager{
    47  		curLexState:     2,
    48  		defaultLexState: 2,
    49  		input_stream:    stream,
    50  		jjrounds:        make([]int, 49),
    51  		jjstateSet:      make([]int, 98),
    52  	}
    53  }
    54  
    55  // L41
    56  
    57  func (tm *TokenManager) jjMoveStringLiteralDfa0_2() int {
    58  	switch tm.curChar {
    59  	case 40:
    60  		panic("not implemented yet")
    61  	case 41:
    62  		panic("not implemented yet")
    63  	case 42:
    64  		panic("not implemented yet")
    65  	case 43:
    66  		panic("not implemented yet")
    67  	case 45:
    68  		panic("not implemented yet")
    69  	case 58:
    70  		panic("not implemented yet")
    71  	case 91:
    72  		panic("not implemented yet")
    73  	case 94:
    74  		panic("not implemented yet")
    75  	case 123:
    76  		panic("not implemented yet")
    77  	default:
    78  		return tm.jjMoveNfa_2(0, 0)
    79  	}
    80  }
    81  
    82  // L87
    83  
    84  func (tm *TokenManager) jjMoveNfa_2(startState, curPos int) int {
    85  	startsAt := 0
    86  	tm.jjnewStateCnt = 49
    87  	i := 1
    88  	tm.jjstateSet[0] = startState
    89  	kind := 0x7fffffff
    90  	for {
    91  		if tm.jjround++; tm.jjround == 0x7fffffff {
    92  			tm.reInitRounds()
    93  		}
    94  		if tm.curChar < 64 {
    95  			l := int64(1 << uint(tm.curChar))
    96  			for {
    97  				i--
    98  				switch tm.jjstateSet[i] {
    99  				case 49, 33:
   100  					if (0xfbff7cf8ffffd9ff & uint64(l)) != 0 {
   101  						if kind > 23 {
   102  							kind = 23
   103  						}
   104  						tm.jjCheckNAddTwoStates(33, 34)
   105  					}
   106  				case 0:
   107  					if (0xfbff54f8ffffd9ff & uint64(l)) != 0 {
   108  						if kind > 23 {
   109  							kind = 23
   110  						}
   111  						tm.jjCheckNAddTwoStates(33, 34)
   112  					} else if (0x100002600 & l) != 0 {
   113  						if kind > 7 {
   114  							kind = 7
   115  						}
   116  					} else if (0x280200000000 & l) != 0 {
   117  						panic("not implemented yet")
   118  					} else if tm.curChar == 47 {
   119  						panic("not implemented yet")
   120  					} else if tm.curChar == 34 {
   121  						panic("not implemented yet")
   122  					}
   123  					if (0x7bff50f8ffffd9ff & l) != 0 {
   124  						if kind > 20 {
   125  							kind = 20
   126  						}
   127  						tm.jjCheckNAddStates(6, 10)
   128  					} else if tm.curChar == 42 {
   129  						panic("not implemented yet")
   130  					} else if tm.curChar == 33 {
   131  						panic("not implemented yet")
   132  					}
   133  					if tm.curChar == 38 {
   134  						panic("not implemented yet")
   135  					}
   136  
   137  				case 4:
   138  					panic("not implemented yet")
   139  				case 5:
   140  					panic("not implemented yet")
   141  				case 13:
   142  					panic("not implemented yet")
   143  				case 14:
   144  					panic("not implemented yet")
   145  				case 15:
   146  					panic("not implemented yet")
   147  				case 16:
   148  					panic("not implemented yet")
   149  				case 17:
   150  					panic("not implemented yet")
   151  				case 19:
   152  					panic("not implemented yet")
   153  				case 20:
   154  					panic("not implemented yet")
   155  				case 22:
   156  					panic("not implemented yet")
   157  				case 23:
   158  					panic("not implemented yet")
   159  				case 24:
   160  					panic("not implemented yet")
   161  				case 25:
   162  					panic("not implemented yet")
   163  				case 27:
   164  					panic("not implemented yet")
   165  				case 28:
   166  					panic("not implemented yet")
   167  				case 30:
   168  					panic("not implemented yet")
   169  				case 31:
   170  					if tm.curChar == 42 && kind > 22 {
   171  						kind = 22
   172  					}
   173  				case 32:
   174  					panic("not implemented yet")
   175  				case 35:
   176  					panic("not implemented yet")
   177  				case 36, 38:
   178  					panic("not implemented yet")
   179  				case 37:
   180  					panic("not implemented yet")
   181  				case 40:
   182  					panic("not implemented yet")
   183  				case 41:
   184  					panic("not implemented yet")
   185  				case 42:
   186  					if (0x7bff78f8ffffd9ff & l) != 0 {
   187  						if kind > 20 {
   188  							kind = 20
   189  						}
   190  						tm.jjCheckNAddTwoStates(42, 43)
   191  					}
   192  				case 44:
   193  					panic("not implemented yet")
   194  				case 45:
   195  					if (0x7bff78f8ffffd9ff & l) != 0 {
   196  						tm.jjCheckNAddStates(18, 20)
   197  					}
   198  				case 47:
   199  					panic("not implemented yet")
   200  				}
   201  				if i == startsAt {
   202  					break
   203  				}
   204  			}
   205  		} else if tm.curChar < 128 {
   206  			l := int64(1) << (uint(tm.curChar) & 077)
   207  			for {
   208  				i--
   209  				switch tm.jjstateSet[i] {
   210  				case 49:
   211  					panic("niy")
   212  				case 0:
   213  					if (0x97ffffff87ffffff & uint64(l)) != 0 {
   214  						if kind > 20 {
   215  							kind = 20
   216  						}
   217  						tm.jjCheckNAddStates(6, 10)
   218  					} else if tm.curChar == 92 {
   219  						panic("niy")
   220  					} else if tm.curChar == 126 {
   221  						panic("niy")
   222  					}
   223  					if (0x97ffffff87ffffff & uint64(l)) != 0 {
   224  						if kind > 23 {
   225  							kind = 23
   226  						}
   227  						tm.jjCheckNAddTwoStates(33, 34)
   228  					}
   229  					switch tm.curChar {
   230  					case 78:
   231  						panic("niy")
   232  					case 124:
   233  						panic("niy")
   234  					case 79:
   235  						panic("niy")
   236  					case 65:
   237  						panic("niy")
   238  					}
   239  				case 1:
   240  					panic("niy")
   241  				case 2:
   242  					panic("niy")
   243  				case 3:
   244  					panic("niy")
   245  				case 6:
   246  					panic("niy")
   247  				case 7:
   248  					panic("niy")
   249  				case 8:
   250  					panic("niy")
   251  				case 9:
   252  					panic("niy")
   253  				case 10:
   254  					panic("niy")
   255  				case 11:
   256  					panic("niy")
   257  				case 12:
   258  					panic("niy")
   259  				case 17:
   260  					panic("niy")
   261  				case 18:
   262  					panic("niy")
   263  				case 19:
   264  					panic("niy")
   265  				case 21:
   266  					panic("niy")
   267  				case 25:
   268  					panic("niy")
   269  				case 26:
   270  					panic("niy")
   271  				case 27:
   272  					panic("niy")
   273  				case 28:
   274  					panic("niy")
   275  				case 29:
   276  					panic("niy")
   277  				case 30:
   278  					panic("niy")
   279  				case 32:
   280  					panic("niy")
   281  				case 33:
   282  					if (0x97ffffff87ffffff & uint64(l)) != 0 {
   283  						if kind > 23 {
   284  							kind = 23
   285  						}
   286  						tm.jjCheckNAddTwoStates(33, 34)
   287  					}
   288  				case 34:
   289  					if tm.curChar == 92 {
   290  						tm.jjCheckNAddTwoStates(35, 35)
   291  					}
   292  				case 35:
   293  					panic("niy")
   294  				case 37:
   295  					panic("niy")
   296  				case 39:
   297  					panic("niy")
   298  				case 41:
   299  					panic("niy")
   300  				case 42:
   301  					if (0x97ffffff87ffffff & uint64(l)) != 0 {
   302  						if kind > 20 {
   303  							kind = 20
   304  						}
   305  						tm.jjCheckNAddTwoStates(42, 43)
   306  					}
   307  				case 43:
   308  					if tm.curChar == 92 {
   309  						tm.jjCheckNAddTwoStates(44, 44)
   310  					}
   311  				case 44:
   312  					panic("niy")
   313  				case 45:
   314  					if (0x97ffffff87ffffff & uint64(l)) != 0 {
   315  						tm.jjCheckNAddStates(18, 20)
   316  					}
   317  				case 46:
   318  					if tm.curChar == 92 {
   319  						tm.jjCheckNAddTwoStates(47, 47)
   320  					}
   321  				case 47:
   322  					panic("niy")
   323  				case 48:
   324  					panic("niy")
   325  				}
   326  				if i <= startsAt { // ==?
   327  					break
   328  				}
   329  			}
   330  		} else {
   331  			hiByte := int(tm.curChar >> 8)
   332  			i1 := hiByte >> 6
   333  			l1 := int64(1 << (uint64(hiByte) & 077))
   334  			i2 := int((tm.curChar & 0xff) >> 6)
   335  			l2 := int64(1 << uint64(tm.curChar&077))
   336  			for {
   337  				i--
   338  				switch tm.jjstateSet[i] {
   339  				case 49, 33:
   340  					if jjCanMove_2(hiByte, i1, i2, l1, l2) {
   341  						if kind > 23 {
   342  							kind = 23
   343  						}
   344  						tm.jjCheckNAddTwoStates(33, 34)
   345  					}
   346  				case 0:
   347  					if jjCanMove_0(hiByte, i1, i2, l1, l2) {
   348  						if kind > 7 {
   349  							kind = 7
   350  						}
   351  					}
   352  					if jjCanMove_2(hiByte, i1, i2, l1, l2) {
   353  						if kind > 23 {
   354  							kind = 23
   355  						}
   356  						tm.jjCheckNAddTwoStates(33, 34)
   357  					}
   358  					if jjCanMove_2(hiByte, i1, i2, l1, l2) {
   359  						if kind > 20 {
   360  							kind = 20
   361  						}
   362  						tm.jjCheckNAddStates(6, 10)
   363  					}
   364  				case 15:
   365  					panic("not implemented yet")
   366  				case 17, 19:
   367  					panic("not implemented yet")
   368  				case 25:
   369  					panic("not implemented yet")
   370  				case 27:
   371  					panic("not implemented yet")
   372  				case 28:
   373  					panic("not implemented yet")
   374  				case 30:
   375  					panic("not implemented yet")
   376  				case 32:
   377  					panic("not implemented yet")
   378  				case 35:
   379  					panic("not implemented yet")
   380  				case 37:
   381  					panic("not implemented yet")
   382  				case 41:
   383  					panic("not implemented yet")
   384  				case 42:
   385  					if jjCanMove_2(hiByte, i1, i2, l1, l2) {
   386  						if kind > 20 {
   387  							kind = 20
   388  						}
   389  						tm.jjCheckNAddTwoStates(42, 43)
   390  					}
   391  				case 44:
   392  					panic("not implemented yet")
   393  				case 45:
   394  					if jjCanMove_2(hiByte, i1, i2, l1, l2) {
   395  						tm.jjCheckNAddStates(18, 20)
   396  					}
   397  				case 47:
   398  					panic("not implemented yet")
   399  				}
   400  				if i == startsAt {
   401  					break
   402  				}
   403  			}
   404  		}
   405  		if kind != 0x7fffffff {
   406  			tm.jjmatchedKind = kind
   407  			tm.jjmatchedPos = curPos
   408  			kind = 0x7fffffff
   409  		}
   410  		curPos++
   411  		i = tm.jjnewStateCnt
   412  		tm.jjnewStateCnt = startsAt
   413  		startsAt = 49 - tm.jjnewStateCnt
   414  		if i == startsAt {
   415  			return curPos
   416  		}
   417  		var err error
   418  		if tm.curChar, err = tm.input_stream.readChar(); err != nil {
   419  			return curPos
   420  		}
   421  	}
   422  	panic("should not be here")
   423  }
   424  
   425  func jjCanMove_0(hiByte, i1, i2 int, l1, l2 int64) bool {
   426  	switch hiByte {
   427  	case 48:
   428  		return (jjbitVec0[i2] & 12) != 0
   429  	}
   430  	return false
   431  }
   432  
   433  func jjCanMove_2(hiByte, i1, i2 int, l1, l2 int64) bool {
   434  	switch hiByte {
   435  	case 0:
   436  		panic("not implemented yet")
   437  	case 48:
   438  		panic("not implemented yet")
   439  	}
   440  	return (jjbitVec4[i1] & uint64(l1)) != 0
   441  }
   442  
   443  func (tm *TokenManager) ReInit(stream CharStream) {
   444  	tm.jjmatchedPos = 0
   445  	tm.jjnewStateCnt = 0
   446  	tm.curLexState = tm.defaultLexState
   447  	tm.input_stream = stream
   448  	tm.reInitRounds()
   449  }
   450  
   451  func (tm *TokenManager) reInitRounds() {
   452  	tm.jjround = 0x80000001
   453  	for i := 48; i >= 0; i-- {
   454  		tm.jjrounds[i] = 0x80000000
   455  	}
   456  }
   457  
   458  // L1027
   459  
   460  func (tm *TokenManager) jjFillToken() *Token {
   461  	var curTokenImage string
   462  	if im, ok := jjstrLiteralImages[tm.jjmatchedKind]; ok {
   463  		curTokenImage = im
   464  	} else {
   465  		curTokenImage = tm.input_stream.image()
   466  	}
   467  	beginLine := tm.input_stream.beginLine()
   468  	beginColumn := tm.input_stream.beginColumn()
   469  	endLine := tm.input_stream.endLine()
   470  	endColumn := tm.input_stream.endColumn()
   471  	t := newToken(tm.jjmatchedKind, curTokenImage)
   472  
   473  	t.beginLine = beginLine
   474  	t.endLine = endLine
   475  	t.beginColumn = beginColumn
   476  	t.endColumn = endColumn
   477  	return t
   478  }
   479  
   480  func (tm *TokenManager) nextToken() (matchedToken *Token) {
   481  	curPos := 0
   482  	var err error
   483  	var eof = false
   484  	for !eof {
   485  		if tm.curChar, err = tm.input_stream.beginToken(); err != nil {
   486  			tm.jjmatchedKind = 0
   487  			matchedToken = tm.jjFillToken()
   488  			return
   489  		}
   490  
   491  		switch tm.curLexState {
   492  		case 0:
   493  			panic("not implemented yet")
   494  		case 1:
   495  			panic("not implemented yet")
   496  		case 2:
   497  			tm.jjmatchedKind = 0x7fffffff
   498  			tm.jjmatchedPos = 0
   499  			curPos = tm.jjMoveStringLiteralDfa0_2()
   500  		}
   501  
   502  		if tm.jjmatchedKind != 0x7fffffff {
   503  			if tm.jjmatchedPos+1 < curPos {
   504  				tm.input_stream.backup(curPos - tm.jjmatchedPos - 1)
   505  			}
   506  			if (jjtoToken[tm.jjmatchedKind>>6] & (int64(1) << uint(tm.jjmatchedKind&077))) != 0 {
   507  				matchedToken = tm.jjFillToken()
   508  				if jjnewLexState[tm.jjmatchedKind] != -1 {
   509  					panic("not implemented yet")
   510  				}
   511  				return matchedToken
   512  			} else {
   513  				if n := jjnewLexState[tm.jjmatchedKind]; n != -1 {
   514  					panic("niy")
   515  					// tm.curLexState = n
   516  				}
   517  				continue
   518  			}
   519  		}
   520  		error_line := tm.input_stream.endLine()
   521  		error_column := tm.input_stream.endColumn()
   522  		var error_after string
   523  		var eofSeen = false
   524  		if _, err = tm.input_stream.readChar(); err == nil {
   525  			tm.input_stream.backup(1)
   526  			if curPos > 1 {
   527  				error_after = tm.input_stream.image()
   528  			}
   529  		} else {
   530  			eofSeen = true
   531  			if curPos > 1 {
   532  				error_after = tm.input_stream.image()
   533  			}
   534  			if tm.curChar == '\n' || tm.curChar == '\r' {
   535  				error_line++
   536  				error_column = 0
   537  			} else {
   538  				error_column++
   539  			}
   540  		}
   541  		panic(newTokenMgrError(eofSeen, tm.curLexState, error_line,
   542  			error_column, error_after, tm.curChar, LEXICAL_ERROR))
   543  	}
   544  	panic("should not be here")
   545  }
   546  
   547  // L1137
   548  func (tm *TokenManager) jjCheckNAdd(state int) {
   549  	if tm.jjrounds[state] != tm.jjround {
   550  		tm.jjstateSet[tm.jjnewStateCnt] = state
   551  		tm.jjnewStateCnt++
   552  		tm.jjrounds[state] = tm.jjround
   553  	}
   554  }
   555  
   556  // L1151
   557  
   558  func (tm *TokenManager) jjCheckNAddTwoStates(state1, state2 int) {
   559  	tm.jjCheckNAdd(state1)
   560  	tm.jjCheckNAdd(state2)
   561  }
   562  
   563  func (tm *TokenManager) jjCheckNAddStates(start, end int) {
   564  	assert(start < end)
   565  	assert(start >= 0)
   566  	assert(end <= len(jjnextStates))
   567  	for {
   568  		tm.jjCheckNAdd(jjnextStates[start])
   569  		start++
   570  		if start >= end {
   571  			break
   572  		}
   573  	}
   574  }
   575  
   576  func assert(ok bool) {
   577  	if !ok {
   578  		panic("assert fail")
   579  	}
   580  }