Skip to content

Commit

Permalink
fix: fix function parse error caused by the wrong modification of the…
Browse files Browse the repository at this point in the history
… previous version
  • Loading branch information
fefit committed Oct 24, 2021
1 parent 6df32d3 commit b22e0cc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
75 changes: 56 additions & 19 deletions fet.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func parseProps(node *Node, defField string) (*Props, error) {
values := []string{}
addProp := func() error {
if contains(props, prop) {
return fmt.Errorf("repeated tag property:'%s'", prop)
return fmt.Errorf("repeated properties: '%s'", prop)
}
if prop == defField {
isHasDefault = true
Expand All @@ -215,7 +215,7 @@ func parseProps(node *Node, defField string) (*Props, error) {
return lastIndex, nil
}
}
return i, fmt.Errorf("wrong quote index:%d", i)
return i, fmt.Errorf("incorrect quote at index:%d", i)
}
isOperator := func(ch string) bool {
return contains([]string{"!", ">", "<", "="}, ch)
Expand All @@ -239,7 +239,7 @@ func parseProps(node *Node, defField string) (*Props, error) {
case isWaitProp:
if s == '"' {
if isHasDefault {
return nil, fmt.Errorf("wrong default prop without property name")
return nil, fmt.Errorf("repeated default properties, maybe you need add a property name for one of them")
}
if lastIndex, err := isQuoteOk(i); err == nil {
defValue := string(rns[i : lastIndex+1])
Expand Down Expand Up @@ -411,27 +411,27 @@ func (node *Node) Compile(options *CompileOptions) (result string, err error) {
case AssignType, OutputType:
isAssign := node.Type == AssignType
if isAssign && !utils.IsIdentifier(name, conf.Mode) {
return "", node.halt("syntax error: wrong variable name '%s', please check the parser mode", name)
return "", node.halt("wrong identifier '%s', please check the parse mode in fet config", name)
}
ast, expErr := exp.Parse(content)
if expErr != nil {
return "", toError(expErr)
}
if isAssign {
if _, ok := generator.LiteralSymbols[name]; ok {
return "", node.halt("syntax error: can not set literal '%s' as a variable name", name)
return "", node.halt("you can't use a literal of '%s' as a variable name", name)
}
symbol := " := "
if contains(currentScopes, name) {
symbol = " = "
}
if compiledText, _, err = gen.Build(ast, genOptions, parseOptions); err != nil {
return "", node.halt("compile error:%s", err.Error())
return "", node.halt("%s", err.Error())
}
result = delimit(addVarPrefix + name + localNS + symbol + compiledText)
} else {
if compiledText, noDelimit, err = gen.Build(ast, genOptions, parseOptions); err != nil {
return "", node.halt("compile error:%s", err.Error())
return "", node.halt("%s", err.Error())
}
if noDelimit {
result = compiledText
Expand All @@ -445,8 +445,11 @@ func (node *Node) Compile(options *CompileOptions) (result string, err error) {
defField := "file"
filename, _ := getStringField(node, defField)
tpl := getRealTplPath(filename, path.Join(node.Pwd, ".."), fet.TemplateDir)
if contains(*includes, tpl) || contains(*extends, tpl) {
return "", node.halt("the include or extends file '%s' has a loop dependence", tpl)
if contains(*includes, tpl) {
return "", node.halt("the 'include' file '%s' cause a circle depencncy.", tpl)
}
if contains(*extends, tpl) {
return "", node.halt("the 'extends' file '%s' cause a circle depencncy.", tpl)
}
incLocalScopes := []string{}
incCaptures := &map[string]string{}
Expand Down Expand Up @@ -480,7 +483,7 @@ func (node *Node) Compile(options *CompileOptions) (result string, err error) {
value := prop.Raw
if ast, expErr := exp.Parse(value); expErr == nil {
if compiledText, _, err = gen.Build(ast, genOptions, parseOptions); err != nil {
return "", node.halt("compile error:%s", err.Error())
return "", node.halt("%s", err.Error())
}
incLocalScopes = append(incLocalScopes, "$"+key)
result += "{{ $" + key + incLocalNS + " := " + compiledText + "}}"
Expand Down Expand Up @@ -661,7 +664,7 @@ func (node *Node) halt(format string, args ...interface{}) error {
if node.Pwd != "" {
errmsg = "[file:'" + node.Pwd + "']"
}
errmsg += "[line:" + indexString(node.LineNo) + ",col:" + indexString(node.StartIndex-node.LineIndex+1) + "]" + fmt.Sprintf(format, args...)
errmsg += "[line:" + indexString(node.LineNo) + ",col:" + indexString(node.StartIndex-node.LineIndex+1) + "][error]" + fmt.Sprintf(format, args...)
return errors.New(errmsg)
}

Expand Down Expand Up @@ -1782,16 +1785,25 @@ func (fet *Fet) RealCmplPath(tpl string) string {
return path.Join(fet.CompileDir, tpl)
}

// ShortTemplPath
func (fet *Fet) ShortTmplPath(tpl string) string {
if shortTpl, err := filepath.Rel(fet.TemplateDir, tpl); err == nil {
return shortTpl
}
return tpl
}

func (fet *Fet) parseFile(tpl string, blocks []*Node, extends *[]string, nested int) (*NodeList, bool, error) {
shortTpl := fet.ShortTmplPath(tpl)
if contains(*extends, tpl) {
return nil, true, fmt.Errorf("the extends file is looped extend:%s", tpl)
return nil, true, fmt.Errorf("The 'extends' file '%s' cause a circle dependency.", shortTpl)
}
isSubTemplate := nested > 0
for {
if _, err := os.Stat(tpl); err == nil {
buf, err := ioutil.ReadFile(tpl)
if err != nil {
return nil, isSubTemplate, fmt.Errorf("read the file failure:%s", tpl)
return nil, isSubTemplate, fmt.Errorf("Open the file '%s' failure: %s", shortTpl, err.Error())
}
content := string(buf)
nl, err := fet.parse(content, tpl)
Expand Down Expand Up @@ -1852,7 +1864,7 @@ func (fet *Fet) parseFile(tpl string, blocks []*Node, extends *[]string, nested
nl.Queues = queues
return nl, isSubTemplate, nil
} else if os.IsNotExist(err) {
return nil, isSubTemplate, fmt.Errorf("the file '%s' is not exists", tpl)
return nil, isSubTemplate, fmt.Errorf("The file '%s' is not exists", tpl)
} else {
return nil, isSubTemplate, err
}
Expand Down Expand Up @@ -1883,6 +1895,12 @@ func (fet *Fet) compileFileContent(tpl string, options *CompileOptions) (string,
return lastCode, nil
}

/**
* fet.getLastDir
* ---------------------------
* get the real directory of fet template
* ---------------------------
*/
func (fet *Fet) getLastDir(dir string) string {
if dir == "" {
return fet.cwd
Expand Down Expand Up @@ -1936,7 +1954,12 @@ func isDelimiterOk(left string, right string) (bool, error) {
return true, nil
}

// CheckConfig if have errors
/**
* fet.CheckConfig
* ---------------------------
* check if a config is ok
* ---------------------------
*/
func (fet *Fet) CheckConfig() error {
if notexist, err := isDorfExists(fet.TemplateDir); err != nil {
if notexist {
Expand Down Expand Up @@ -1965,7 +1988,12 @@ func isDorfExists(pathname string) (notexist bool, err error) {
return false, nil
}

// Compile file
/**
* fet.Compile
* ---------------------------
* compile one template file
* ---------------------------
*/
func (fet *Fet) Compile(tpl string, writeFile bool) (string, []string, error) {
var (
result string
Expand Down Expand Up @@ -2092,20 +2120,28 @@ func (fet *Fet) GetCompileFiles(dorf string) ([]string, error) {
return fileList, nil
}

// CompileAll files
/**
* fet.CompileAll
* ---------------------------
* compile all the files
* ---------------------------
*/
func (fet *Fet) CompileAll() (*sync.Map, error) {
var (
relations sync.Map
deps []string
)
// list all the files need compile
files, err := fet.dirCompiledFiles(fet.TemplateDir)
if err != nil {
return &relations, fmt.Errorf("sorry,fail to open the compile directory:%s", err.Error())
return &relations, fmt.Errorf("Fail to open the template directory: %s", err.Error())
}
// if no files need compile
total := len(files)
if total == 0 {
return &relations, nil
}
// if only one file
if total == 1 {
_, deps, err = fet.Compile(files[0], true)
if err == nil {
Expand All @@ -2117,6 +2153,7 @@ func (fet *Fet) CompileAll() (*sync.Map, error) {
wg sync.WaitGroup
errs []string
)
// compile the files concurrently
wg.Add(total)
for _, tpl := range files {
go func(tpl string, fet *Fet) {
Expand All @@ -2132,7 +2169,7 @@ func (fet *Fet) CompileAll() (*sync.Map, error) {
}
wg.Wait()
if errs != nil {
return &relations, fmt.Errorf("compile file error:%s", strings.Join(errs, "\n"))
return &relations, fmt.Errorf("Compile error:%s", strings.Join(errs, "\n"))
}
return &relations, nil
}
15 changes: 8 additions & 7 deletions lib/expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1442,13 +1442,13 @@ func (exp *Expression) toAst(tokens []AnyToken, isInFunc bool) (*TokenNode, erro
parsed = append(parsed, token)
}
}
//
// if only one token
if len(parsed) == 1 {
curToken := parsed[0]
if result, ok := curToken.(*TokenNode); ok {
return result, nil
}
return nil, fmt.Errorf("unexpect token,%#v", curToken)
return nil, fmt.Errorf("Unexpected token:%#v", curToken)
}
/**
* 4. the forth step
Expand Down Expand Up @@ -1531,15 +1531,16 @@ func (exp *Expression) toAst(tokens []AnyToken, isInFunc bool) (*TokenNode, erro
}
}
}
// if is in func less than one parameters
/**
* if is in a function node and only one argument
* e.g. => call($a - $b)
*/
if isInFunc && !isArg {
fnNode := &Node{
Type: "function",
}
if result.Type != "" {
fnNode.Arguments = append(fnNode.Arguments, result)
result.Function = fnNode
}
fnNode.Arguments = append(fnNode.Arguments, result)
result.Function = fnNode
return &TokenNode{
Token: lastToken,
Node: fnNode,
Expand Down

0 comments on commit b22e0cc

Please sign in to comment.