1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-26 20:28:49 +08:00

Expose Parser Interface with a Scan method

This commit is contained in:
Amjad Masad
2016-04-24 23:01:26 -07:00
parent c619b8fa10
commit cb833c2321

View File

@@ -86,6 +86,10 @@ type _parser struct {
skippedLineBreak bool
}
type Parser interface {
Scan() (tkn token.Token, literal string, idx file.Idx)
}
func _newParser(filename, src string, base int) *_parser {
return &_parser{
chr: ' ', // This is set so we can start scanning by skipping whitespace
@@ -103,6 +107,11 @@ func newParser(filename, src string) *_parser {
return _newParser(filename, src, 1)
}
// Returns a new Parser.
func NewParser(filename, src string) Parser {
return newParser(filename, src)
}
func ReadSource(filename string, src interface{}) ([]byte, error) {
if src != nil {
switch src := src.(type) {
@@ -176,6 +185,13 @@ func ParseFunction(parameterList, body string) (*ast.FunctionLiteral, error) {
return program.Body[0].(*ast.ExpressionStatement).Expression.(*ast.FunctionLiteral), nil
}
// Scan reads a single token from the source at the current offset, increments the offset and
// returns the token.Token token, a string literal representing the value of the token (if applicable)
// and it's current file.Idx index.
func (self *_parser) Scan() (tkn token.Token, literal string, idx file.Idx) {
return self.scan()
}
func (self *_parser) slice(idx0, idx1 file.Idx) string {
from := int(idx0) - self.base
to := int(idx1) - self.base