Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

parser.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright (c) 2016 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package camelcase
  15. import (
  16. "github.com/blevesearch/bleve/analysis"
  17. )
  18. func (p *Parser) buildTokenFromTerm(buffer []rune) *analysis.Token {
  19. term := analysis.BuildTermFromRunes(buffer)
  20. token := &analysis.Token{
  21. Term: term,
  22. Position: p.position,
  23. Start: p.index,
  24. End: p.index + len(term),
  25. }
  26. p.position++
  27. p.index += len(term)
  28. return token
  29. }
  30. // Parser accepts a symbol and passes it to the current state (representing a class).
  31. // The state can accept it (and accumulate it). Otherwise, the parser creates a new state that
  32. // starts with the pushed symbol.
  33. //
  34. // Parser accumulates a new resulting token every time it switches state.
  35. // Use FlushTokens() to get the results after the last symbol was pushed.
  36. type Parser struct {
  37. bufferLen int
  38. buffer []rune
  39. current State
  40. tokens []*analysis.Token
  41. position int
  42. index int
  43. }
  44. func NewParser(len, position, index int) *Parser {
  45. return &Parser{
  46. bufferLen: len,
  47. buffer: make([]rune, 0, len),
  48. tokens: make([]*analysis.Token, 0, len),
  49. position: position,
  50. index: index,
  51. }
  52. }
  53. func (p *Parser) Push(sym rune, peek *rune) {
  54. if p.current == nil {
  55. // the start of parsing
  56. p.current = p.NewState(sym)
  57. p.buffer = append(p.buffer, sym)
  58. } else if p.current.Member(sym, peek) {
  59. // same state, just accumulate
  60. p.buffer = append(p.buffer, sym)
  61. } else {
  62. // the old state is no more, thus convert the buffer
  63. p.tokens = append(p.tokens, p.buildTokenFromTerm(p.buffer))
  64. // let the new state begin
  65. p.current = p.NewState(sym)
  66. p.buffer = make([]rune, 0, p.bufferLen)
  67. p.buffer = append(p.buffer, sym)
  68. }
  69. }
  70. // Note. States have to have different starting symbols.
  71. func (p *Parser) NewState(sym rune) State {
  72. var found State
  73. found = &LowerCaseState{}
  74. if found.StartSym(sym) {
  75. return found
  76. }
  77. found = &UpperCaseState{}
  78. if found.StartSym(sym) {
  79. return found
  80. }
  81. found = &NumberCaseState{}
  82. if found.StartSym(sym) {
  83. return found
  84. }
  85. return &NonAlphaNumericCaseState{}
  86. }
  87. func (p *Parser) FlushTokens() []*analysis.Token {
  88. p.tokens = append(p.tokens, p.buildTokenFromTerm(p.buffer))
  89. return p.tokens
  90. }
  91. func (p *Parser) NextPosition() int {
  92. return p.position
  93. }