1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-26 06:17:03 +00:00
Former-commit-id: c6f2c47cece55865b381df33950e6dfd8ccf882f
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-03-27 14:38:10 +03:00
parent a95a02a16a
commit 251eeb6bd0
4 changed files with 136 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ package parser
import (
"fmt"
"strconv"
"strings"
"gopkg.in/kataras/iris.v6/_future/ipel/ast"
@@ -26,8 +27,20 @@ func (p *Parser) appendErr(format string, a ...interface{}) {
p.errors = append(p.errors, fmt.Sprintf(format, a...))
}
const DefaultParamErrorCode = 404
func parseParamFuncArg(t token.Token) (a ast.ParamFuncArg, err error) {
if t.Type == token.INT {
return ast.ParamFuncArgToInt(t.Literal)
}
return t.Literal, 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)
lastParamFunc := ast.ParamFunc{}
for {
t := p.l.NextToken()
if t.Type == token.EOF {
@@ -45,11 +58,52 @@ 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
case token.IDENT:
lastParamFunc.Name = t.Literal
case token.LPAREN:
argValTok := p.l.NextToken()
argVal, err := parseParamFuncArg(argValTok)
if err != nil {
p.appendErr("[%d:%d] expected param func argument to be an integer but got %s", t.Start, t.End, argValTok.Literal)
continue
}
lastParamFunc.Args = append(lastParamFunc.Args, argVal)
case token.COMMA:
argValTok := p.l.NextToken()
argVal, err := parseParamFuncArg(argValTok)
if err != nil {
p.appendErr("[%d:%d] expected param func argument to be an integer but got %s", t.Start, t.End, argValTok.Literal)
continue
}
lastParamFunc.Args = append(lastParamFunc.Args, argVal)
case token.RPAREN:
stmt.Funcs = append(stmt.Funcs, lastParamFunc)
lastParamFunc = ast.ParamFunc{} // reset
case token.ELSE:
errCodeTok := p.l.NextToken()
if errCodeTok.Type != token.INT {
p.appendErr("[%d:%d] expected error code to be an integer but got %s", t.Start, t.End, errCodeTok.Literal)
continue
}
errCode, err := strconv.Atoi(errCodeTok.Literal)
if err != nil {
// this is a bug on lexer if throws because we already check for token.INT
p.appendErr("[%d:%d] unexpected lexer error while trying to convert error code to an integer, %s", t.Start, t.End, err.Error())
continue
}
stmt.ErrorCode = errCode
case token.RBRACE:
break
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)
p.appendErr("[%d:%d] unexpected token type: %q with value %s", t.Start, t.End, t.Type, t.Literal)
}
}