Fix linter issues on the parser module
This commit is contained in:
parent
b650a10ba0
commit
30669d9327
|
@ -112,7 +112,7 @@ func (sp *StringParser) next(skipWhitespace bool) *string {
|
||||||
}
|
}
|
||||||
char := sp.buffer[sp.pos]
|
char := sp.buffer[sp.pos]
|
||||||
sp.updateLineIndex(sp.pos)
|
sp.updateLineIndex(sp.pos)
|
||||||
sp.pos = sp.pos + 1
|
sp.pos++
|
||||||
|
|
||||||
if skipWhitespace && isSeparator(&char) {
|
if skipWhitespace && isSeparator(&char) {
|
||||||
return sp.next(skipWhitespace)
|
return sp.next(skipWhitespace)
|
||||||
|
@ -146,7 +146,7 @@ func (sp *StringParser) peek(skipWhitespace bool) *string {
|
||||||
c := sp.buffer[sp.pos]
|
c := sp.buffer[sp.pos]
|
||||||
if isSeparator(&c) && skipWhitespace {
|
if isSeparator(&c) && skipWhitespace {
|
||||||
sp.updateLineIndex(sp.pos)
|
sp.updateLineIndex(sp.pos)
|
||||||
sp.pos = sp.pos + 1
|
sp.pos++
|
||||||
return sp.peek(skipWhitespace)
|
return sp.peek(skipWhitespace)
|
||||||
}
|
}
|
||||||
return &c
|
return &c
|
||||||
|
@ -155,7 +155,7 @@ func (sp *StringParser) peek(skipWhitespace bool) *string {
|
||||||
// Move the char pointer back by one character
|
// Move the char pointer back by one character
|
||||||
func (sp *StringParser) back() {
|
func (sp *StringParser) back() {
|
||||||
if sp.pos > 0 {
|
if sp.pos > 0 {
|
||||||
sp.pos = sp.pos - 1
|
sp.pos--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,7 +215,7 @@ func readKeyword(parser IParsable) (IExpr, IError) {
|
||||||
return MakeKeyword(node, ":"+symbol.(*Symbol).String())
|
return MakeKeyword(node, ":"+symbol.(*Symbol).String())
|
||||||
}
|
}
|
||||||
|
|
||||||
//readRawSymbol reads a symbol from the current position forward
|
// readRawSymbol reads a symbol from the current position forward
|
||||||
func readRawSymbol(parser IParsable) (IExpr, IError) {
|
func readRawSymbol(parser IParsable) (IExpr, IError) {
|
||||||
c := parser.peek(false)
|
c := parser.peek(false)
|
||||||
var symbol string
|
var symbol string
|
||||||
|
@ -245,7 +245,7 @@ func readRawSymbol(parser IParsable) (IExpr, IError) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if isValidForSymbol(*c) {
|
if isValidForSymbol(*c) {
|
||||||
symbol = symbol + *c
|
symbol += *c
|
||||||
} else {
|
} else {
|
||||||
parser.back()
|
parser.back()
|
||||||
break
|
break
|
||||||
|
@ -281,20 +281,20 @@ func readString(parser IParsable) (IExpr, IError) {
|
||||||
c = parser.next(false)
|
c = parser.next(false)
|
||||||
switch *c {
|
switch *c {
|
||||||
case "n":
|
case "n":
|
||||||
str = str + "\n"
|
str += "\n"
|
||||||
case "t":
|
case "t":
|
||||||
str = str + "\t"
|
str += "\t"
|
||||||
case "r":
|
case "r":
|
||||||
str = str + "\r"
|
str += "\r"
|
||||||
case "\\":
|
case "\\":
|
||||||
str = str + "\\"
|
str += "\\"
|
||||||
case "\"":
|
case "\"":
|
||||||
str = str + "\""
|
str += "\""
|
||||||
default:
|
default:
|
||||||
return nil, makeErrorAtPoint(parser, "Unsupported escape character: \\%s", *c)
|
return nil, makeErrorAtPoint(parser, "Unsupported escape character: \\%s", *c)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
str = str + *c
|
str += *c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -322,7 +322,7 @@ func readNumber(parser IParsable, neg bool) (IExpr, IError) {
|
||||||
|
|
||||||
if *c == "." {
|
if *c == "." {
|
||||||
isDouble = true
|
isDouble = true
|
||||||
result = result + *c
|
result += *c
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -330,7 +330,7 @@ func readNumber(parser IParsable, neg bool) (IExpr, IError) {
|
||||||
char := *c
|
char := *c
|
||||||
r := rune(char[0])
|
r := rune(char[0])
|
||||||
if unicode.IsDigit(r) {
|
if unicode.IsDigit(r) {
|
||||||
result = result + *c
|
result += *c
|
||||||
} else if isValidForSymbol(char) {
|
} else if isValidForSymbol(char) {
|
||||||
return nil, makeErrorAtPoint(parser, "Illegal token while scanning for a number.")
|
return nil, makeErrorAtPoint(parser, "Illegal token while scanning for a number.")
|
||||||
} else {
|
} else {
|
||||||
|
@ -382,11 +382,10 @@ func readSymbol(parser IParsable) (IExpr, IError) {
|
||||||
|
|
||||||
if unicode.IsDigit(r) {
|
if unicode.IsDigit(r) {
|
||||||
return readNumber(parser, true)
|
return readNumber(parser, true)
|
||||||
} else {
|
|
||||||
// Unread '-'
|
|
||||||
parser.back()
|
|
||||||
return readRawSymbol(parser)
|
|
||||||
}
|
}
|
||||||
|
// Unread '-'
|
||||||
|
parser.back()
|
||||||
|
return readRawSymbol(parser)
|
||||||
}
|
}
|
||||||
return readRawSymbol(parser)
|
return readRawSymbol(parser)
|
||||||
}
|
}
|
||||||
|
@ -435,28 +434,25 @@ func readComment(parser IParsable) (IExpr, IError) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// readQuotedExpr reads quoted expression ( lie 'something ) by replaceing the
|
// readQuotedExpr reads the backquote and replace it with a call
|
||||||
// quote with a call to `quote` special form so 'something => (quote something)
|
// to the `quasiquote` macro.
|
||||||
func readQuotedExpr(parser IParsable) (IExpr, IError) {
|
func readQuotedExpr(parser IParsable, quote string) (IExpr, IError) {
|
||||||
expr, err := readExpr(parser)
|
expr, err := readExpr(parser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
symNode := MakeNode(parser.GetSource(), parser.GetLocation(), parser.GetLocation())
|
node := MakeNode(parser.GetSource(), parser.GetLocation(), parser.GetLocation())
|
||||||
sym, err := MakeSymbol(symNode, "quote")
|
sym, err := MakeSymbol(node, quote)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err.SetNode(&symNode)
|
err.SetNode(&node)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
listElems := []IExpr{
|
listElems := []IExpr{sym, expr}
|
||||||
sym,
|
|
||||||
expr,
|
|
||||||
}
|
|
||||||
|
|
||||||
listNode := MakeNodeFromExprs(listElems)
|
listNode := MakeNodeFromExprs(listElems)
|
||||||
|
// listNode won't be nil in this case but it doesn't
|
||||||
|
// mean we shouldn't check
|
||||||
if listNode == nil {
|
if listNode == nil {
|
||||||
n := MakeSinglePointNode(parser.GetSource(), parser.GetLocation())
|
n := MakeSinglePointNode(parser.GetSource(), parser.GetLocation())
|
||||||
listNode = &n
|
listNode = &n
|
||||||
|
@ -464,6 +460,7 @@ func readQuotedExpr(parser IParsable) (IExpr, IError) {
|
||||||
|
|
||||||
listNode.location.DecStart(1)
|
listNode.location.DecStart(1)
|
||||||
listNode.location.IncStart(1)
|
listNode.location.IncStart(1)
|
||||||
|
|
||||||
return MakeList(*listNode, listElems), nil
|
return MakeList(*listNode, listElems), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -522,36 +519,6 @@ func readUnquotedExpr(parser IParsable) (IExpr, IError) {
|
||||||
return MakeList(*listNode, listElems), nil
|
return MakeList(*listNode, listElems), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// readQuasiquotedExpr reads the backquote and replace it with a call
|
|
||||||
// to the `quasiquote` macro.
|
|
||||||
func readQuasiquotedExpr(parser IParsable) (IExpr, IError) {
|
|
||||||
expr, err := readExpr(parser)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
node := MakeNode(parser.GetSource(), parser.GetLocation(), parser.GetLocation())
|
|
||||||
sym, err := MakeSymbol(node, "quasiquote")
|
|
||||||
if err != nil {
|
|
||||||
err.SetNode(&node)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
listElems := []IExpr{sym, expr}
|
|
||||||
listNode := MakeNodeFromExprs(listElems)
|
|
||||||
// listNode won't be nil in this case but it doesn't
|
|
||||||
// mean we shouldn't check
|
|
||||||
if listNode == nil {
|
|
||||||
n := MakeSinglePointNode(parser.GetSource(), parser.GetLocation())
|
|
||||||
listNode = &n
|
|
||||||
}
|
|
||||||
|
|
||||||
listNode.location.DecStart(1)
|
|
||||||
listNode.location.IncStart(1)
|
|
||||||
|
|
||||||
return MakeList(*listNode, listElems), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// readExpr reads one expression from the input. This function is the most
|
// readExpr reads one expression from the input. This function is the most
|
||||||
// important function in the parser which dispatches the call to different
|
// important function in the parser which dispatches the call to different
|
||||||
// reader functions based on the first character
|
// reader functions based on the first character
|
||||||
|
@ -565,7 +532,7 @@ loop:
|
||||||
}
|
}
|
||||||
|
|
||||||
if *c == "'" {
|
if *c == "'" {
|
||||||
return readQuotedExpr(parser)
|
return readQuotedExpr(parser, "quote")
|
||||||
}
|
}
|
||||||
|
|
||||||
if *c == "~" {
|
if *c == "~" {
|
||||||
|
@ -573,7 +540,7 @@ loop:
|
||||||
}
|
}
|
||||||
|
|
||||||
if *c == "`" {
|
if *c == "`" {
|
||||||
return readQuasiquotedExpr(parser)
|
return readQuotedExpr(parser, "quasiquote")
|
||||||
}
|
}
|
||||||
if *c == "(" {
|
if *c == "(" {
|
||||||
return readList(parser)
|
return readList(parser)
|
||||||
|
@ -586,25 +553,19 @@ loop:
|
||||||
if *c == ":" {
|
if *c == ":" {
|
||||||
return readKeyword(parser)
|
return readKeyword(parser)
|
||||||
}
|
}
|
||||||
// if *c == "[" {
|
|
||||||
// readVector(parser)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if *c == "{" {
|
|
||||||
// readMap(parser)
|
|
||||||
// }
|
|
||||||
parser.back()
|
parser.back()
|
||||||
return readSymbol(parser)
|
return readSymbol(parser)
|
||||||
}
|
}
|
||||||
|
|
||||||
//ParseToAST is the entry function to the reader/parser which
|
// ParseToAST is the entry function to the reader/parser which
|
||||||
// converts the `input` string to a `Block` of code. A block
|
// converts the `input` string to a `Block` of code. A block
|
||||||
// by itself is not something available to the language. It's
|
// by itself is not something available to the language. It's
|
||||||
// just anbstraction for a ordered collection of expressions.
|
// just anbstraction for a ordered collection of expressions.
|
||||||
// It doesn't have anything to do with the concept of blocks
|
// It doesn't have anything to do with the concept of blocks
|
||||||
// from other programming languages.
|
// from other programming languages.
|
||||||
func ParseToAST(ns string, input string) (*Block, IError) {
|
func ParseToAST(ns, input string) (*Block, IError) {
|
||||||
var ast Block
|
var forms Block
|
||||||
parser := StringParser{
|
parser := StringParser{
|
||||||
buffer: strings.Split(input, ""),
|
buffer: strings.Split(input, ""),
|
||||||
pos: 0,
|
pos: 0,
|
||||||
|
@ -621,8 +582,8 @@ func ParseToAST(ns string, input string) (*Block, IError) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
ast.Append(expr)
|
forms.Append(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ast, nil
|
return &forms, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue