forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,80 @@ | ||
package prompt | ||
|
||
// LexerFunc is a callback from render. | ||
type LexerFunc = func(line string) []LexerElement | ||
// Lexer is a streaming lexer that takes in a piece of text | ||
// and streams tokens with the Next() method | ||
type Lexer interface { | ||
Init(string) // Reset the lexer's state and initialise it with the given input. | ||
// Next returns the next Token and a bool flag | ||
// which is false when the end of input has been reached. | ||
Next() (Token, bool) | ||
} | ||
|
||
// LexerElement is a element of lexer. | ||
type LexerElement struct { | ||
Color Color | ||
Text string | ||
// Token is a single unit of text returned by a Lexer. | ||
type Token interface { | ||
Color() Color | ||
Lexeme() string // original string that matches this token | ||
} | ||
|
||
// Lexer is a struct with lexer param and function. | ||
type Lexer struct { | ||
IsEnabled bool | ||
fn LexerFunc | ||
// SimpleToken as the default implementation of Token. | ||
type SimpleToken struct { | ||
color Color | ||
lexeme string | ||
} | ||
|
||
// NewLexer returns new Lexer. | ||
func NewLexer() *Lexer { | ||
return &Lexer{ | ||
IsEnabled: false, | ||
// Create a new SimpleToken. | ||
func NewSimpleToken(color Color, lexeme string) *SimpleToken { | ||
return &SimpleToken{ | ||
color: color, | ||
lexeme: lexeme, | ||
} | ||
} | ||
|
||
// SetLexerFunction in lexer struct. | ||
func (l *Lexer) SetLexerFunction(fn LexerFunc) { | ||
l.IsEnabled = true | ||
l.fn = fn | ||
// Retrieve the color of this token. | ||
func (t *SimpleToken) Color() Color { | ||
return t.color | ||
} | ||
|
||
// Retrieve the text that this token represents. | ||
func (t *SimpleToken) Lexeme() string { | ||
return t.lexeme | ||
} | ||
|
||
// Process line with a custom function. | ||
func (l *Lexer) Process(line string) []LexerElement { | ||
return l.fn(line) | ||
// LexerFunc is a function implementing | ||
// a simple lexer that receives a string | ||
// and returns a complete slice of Tokens. | ||
type LexerFunc func(string) []Token | ||
|
||
// EagerLexer is a wrapper around LexerFunc that | ||
// transforms an eager lexer which produces an | ||
// array with all tokens at once into a streaming | ||
// lexer compatible with go-prompt. | ||
type EagerLexer struct { | ||
lexFunc LexerFunc | ||
tokens []Token | ||
currentIndex int | ||
} | ||
|
||
// Create a new EagerLexer. | ||
func NewEagerLexer(fn LexerFunc) *EagerLexer { | ||
return &EagerLexer{ | ||
lexFunc: fn, | ||
} | ||
} | ||
|
||
// Initialise the lexer with the given input. | ||
func (l *EagerLexer) Init(input string) { | ||
l.tokens = l.lexFunc(input) | ||
l.currentIndex = 0 | ||
} | ||
|
||
// Return the next token and true if the operation | ||
// was successful. | ||
func (l *EagerLexer) Next() (Token, bool) { | ||
if l.currentIndex >= len(l.tokens) { | ||
return nil, false | ||
} | ||
|
||
result := l.tokens[l.currentIndex] | ||
l.currentIndex++ | ||
return result, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters