mirror of
https://github.com/kataras/iris.git
synced 2025-12-25 13:57:04 +00:00
Nothing special here (yet)
Former-commit-id: 826d7c370481b78afd9ba92f4ae8bef1fb85a567
This commit is contained in:
61
_future/ipel/parser/parser.go
Normal file
61
_future/ipel/parser/parser.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/kataras/iris.v6/_future/ipel/ast"
|
||||
"gopkg.in/kataras/iris.v6/_future/ipel/lexer"
|
||||
"gopkg.in/kataras/iris.v6/_future/ipel/token"
|
||||
)
|
||||
|
||||
type Parser struct {
|
||||
l *lexer.Lexer
|
||||
errors []string
|
||||
}
|
||||
|
||||
func New(lexer *lexer.Lexer) *Parser {
|
||||
p := &Parser{
|
||||
l: lexer,
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Parser) appendErr(format string, a ...interface{}) {
|
||||
p.errors = append(p.errors, fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
func (p *Parser) Parse() (*ast.ParamStatement, error) {
|
||||
stmt := new(ast.ParamStatement)
|
||||
for {
|
||||
t := p.l.NextToken()
|
||||
if t.Type == token.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
switch t.Type {
|
||||
case token.LBRACE:
|
||||
// name
|
||||
nextTok := p.l.NextToken()
|
||||
stmt.Name = nextTok.Literal
|
||||
case token.COLON:
|
||||
// type
|
||||
nextTok := p.l.NextToken()
|
||||
paramType := ast.LookupParamType(nextTok.Literal)
|
||||
if paramType == ast.ParamTypeUnExpected {
|
||||
p.appendErr("[%d:%d] unexpected parameter type: %s", t.Start, t.End, nextTok.Literal)
|
||||
}
|
||||
case token.ILLEGAL:
|
||||
p.appendErr("[%d:%d] illegal token: %s", t.Start, t.End, t.Literal)
|
||||
default:
|
||||
p.appendErr("[%d:%d] unexpected token type: %q", t.Start, t.End, t.Type)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if len(p.errors) > 0 {
|
||||
return nil, fmt.Errorf(strings.Join(p.errors, "\n"))
|
||||
}
|
||||
return stmt, nil
|
||||
}
|
||||
48
_future/ipel/parser/parser_test.go
Normal file
48
_future/ipel/parser/parser_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/kataras/iris.v6/_future/ipel/lexer"
|
||||
)
|
||||
|
||||
// Test is failing because we are not finished with the Parser yet
|
||||
// 27/03
|
||||
func TestParseError(t *testing.T) {
|
||||
// fail
|
||||
illegalChar := '$'
|
||||
|
||||
input := "{id" + string(illegalChar) + "int range(1,5) else 404}"
|
||||
l := lexer.New(input)
|
||||
p := New(l)
|
||||
|
||||
_, err := p.Parse()
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("expecting not empty error on input '%s'", input)
|
||||
}
|
||||
|
||||
// println(input[8:9])
|
||||
// println(input[13:17])
|
||||
|
||||
illIdx := strings.IndexRune(input, illegalChar)
|
||||
expectedErr := fmt.Sprintf("[%d:%d] illegal token: %s", illIdx, illIdx, "$")
|
||||
if got := err.Error(); got != expectedErr {
|
||||
t.Fatalf("expecting error to be '%s' but got: %s", expectedErr, got)
|
||||
}
|
||||
//
|
||||
|
||||
// success
|
||||
input2 := "{id:int range(1,5) else 404}"
|
||||
l2 := lexer.New(input2)
|
||||
p2 := New(l2)
|
||||
|
||||
_, err = p2.Parse()
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expecting empty error on input '%s', but got: %s", input2, err.Error())
|
||||
}
|
||||
//
|
||||
}
|
||||
Reference in New Issue
Block a user