1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-25 22:07:03 +00:00
1. Fix index, including both start and end. So Literal[start:end+1] will
be a valid part.

2. Replace any with string, add file param type

3. Start of making the evaluator, starting with regexp for param types
(these expression can be changed or/and overriden by user later on)


Former-commit-id: ab95265f953dadbf84170b543e1ff8840f9c4a14
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-03-27 22:33:19 +03:00
parent 251eeb6bd0
commit 126c4de29b
8 changed files with 158 additions and 55 deletions

View File

@@ -15,19 +15,23 @@ type Parser struct {
errors []string
}
func New(lexer *lexer.Lexer) *Parser {
p := &Parser{
l: lexer,
}
func New(src string) *Parser {
p := new(Parser)
p.Reset(src)
return p
}
func (p *Parser) Reset(src string) {
p.l = lexer.New(src)
p.errors = []string{}
}
func (p *Parser) appendErr(format string, a ...interface{}) {
p.errors = append(p.errors, fmt.Sprintf(format, a...))
}
const DefaultParamErrorCode = 404
const DefaultParamType = ast.ParamTypeString
func parseParamFuncArg(t token.Token) (a ast.ParamFuncArg, err error) {
if t.Type == token.INT {
@@ -36,20 +40,35 @@ func parseParamFuncArg(t token.Token) (a ast.ParamFuncArg, err error) {
return t.Literal, nil
}
func (p Parser) Error() error {
if len(p.errors) > 0 {
return fmt.Errorf(strings.Join(p.errors, "\n"))
}
return nil
}
func (p *Parser) Parse() (*ast.ParamStatement, error) {
stmt := new(ast.ParamStatement)
stmt.ErrorCode = DefaultParamErrorCode
// let's have them nilled stmt.Funcs = make([]ast.ParamFunc, 0)
p.errors = []string{}
stmt := &ast.ParamStatement{
ErrorCode: DefaultParamErrorCode,
Type: DefaultParamType,
}
lastParamFunc := ast.ParamFunc{}
for {
t := p.l.NextToken()
if t.Type == token.EOF {
if stmt.Name == "" {
p.appendErr("[1:] parameter name is missing")
}
break
}
switch t.Type {
case token.LBRACE:
// name
// name, alphabetical and _, param names are not allowed to contain any number.
nextTok := p.l.NextToken()
stmt.Name = nextTok.Literal
case token.COLON:
@@ -58,7 +77,6 @@ func (p *Parser) Parse() (*ast.ParamStatement, error) {
paramType := ast.LookupParamType(nextTok.Literal)
if paramType == ast.ParamTypeUnExpected {
p.appendErr("[%d:%d] unexpected parameter type: %s", t.Start, t.End, nextTok.Literal)
continue
}
stmt.Type = paramType
// param func
@@ -104,12 +122,8 @@ func (p *Parser) Parse() (*ast.ParamStatement, error) {
p.appendErr("[%d:%d] illegal token: %s", t.Start, t.End, t.Literal)
default:
p.appendErr("[%d:%d] unexpected token type: %q with value %s", t.Start, t.End, t.Type, t.Literal)
}
}
if len(p.errors) > 0 {
return nil, fmt.Errorf(strings.Join(p.errors, "\n"))
}
return stmt, nil
return stmt, p.Error()
}

View File

@@ -7,7 +7,6 @@ import (
"testing"
"gopkg.in/kataras/iris.v6/_future/ipel/ast"
"gopkg.in/kataras/iris.v6/_future/ipel/lexer"
)
func TestParseError(t *testing.T) {
@@ -15,8 +14,7 @@ func TestParseError(t *testing.T) {
illegalChar := '$'
input := "{id" + string(illegalChar) + "int range(1,5) else 404}"
l := lexer.New(input)
p := New(l)
p := New(input)
_, err := p.Parse()
@@ -36,10 +34,8 @@ func TestParseError(t *testing.T) {
// success
input2 := "{id:int range(1,5) else 404}"
l2 := lexer.New(input2)
p2 := New(l2)
_, err = p2.Parse()
p.Reset(input2)
_, err = p.Parse()
if err != nil {
t.Fatalf("expecting empty error on input '%s', but got: %s", input2, err.Error())
@@ -89,23 +85,35 @@ func TestParse(t *testing.T) {
},
ErrorCode: 404,
}}, // 2
{"{username:alphabetical", true,
{"{username:alphabetical}", true,
ast.ParamStatement{
Name: "username",
Type: ast.ParamTypeAlphabetical,
ErrorCode: 404,
}}, // 3
{"{username:thisianunexpected", false,
{"{myparam}", true,
ast.ParamStatement{
Name: "username",
Type: ast.ParamTypeUnExpected,
Name: "myparam",
Type: ast.ParamTypeString,
ErrorCode: 404,
}}, // 4
}
{"{myparam_:thisianunexpected}", false,
ast.ParamStatement{
Name: "myparam_",
Type: ast.ParamTypeUnExpected,
ErrorCode: 404,
}}, // 5
{"{myparam2}", false, // false because it will give an error of unexpeced token type with value 2
ast.ParamStatement{
Name: "myparam", // expected "myparam" because we don't allow integers to the parameter names.
Type: ast.ParamTypeString,
ErrorCode: 404,
}}, // 6
}
var p *Parser = new(Parser)
for i, tt := range tests {
l := lexer.New(tt.input)
p := New(l)
p.Reset(tt.input)
resultStmt, err := p.Parse()
if tt.valid && err != nil {