Skip to content

Commit

Permalink
development: current entity name can be provided to DST function
Browse files Browse the repository at this point in the history
  • Loading branch information
onokonem committed Mar 2, 2019
1 parent 7a2ff8a commit c1d3e66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
33 changes: 7 additions & 26 deletions internal/frontend/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,14 @@ func (T *Token) UintValue() (uint64, error) {
return strconv.ParseUint(string(T.Lit), 10, 64)
}

var (
sdtPlaceholders = regexp.MustCompile(`\$(\d+)`)
sdtReplacement = []byte("X[$1]")
)

func (T *Token) SDTVal() string {
sdt := string(T.Lit)
rex, err := regexp.Compile("\\$[0-9]+")
if err != nil {
panic(err)
}
idx := rex.FindAllStringIndex(sdt, -1)
res := ""
if len(idx) <= 0 {
res = sdt
} else {
for i, loc := range idx {
if loc[0] > 0 {
if i > 0 {
res += sdt[idx[i-1][1]:loc[0]]
} else {
res += sdt[0:loc[0]]
}
}
res += "X["
res += sdt[loc[0]+1 : loc[1]]
res += "]"
}
if idx[len(idx)-1][1] < len(sdt) {
res += sdt[idx[len(idx)-1][1]:]
}
}
res := string(sdtPlaceholders.ReplaceAll(T.Lit, sdtReplacement))

return strings.TrimSpace(res[2 : len(res)-2])
}

Expand Down
8 changes: 7 additions & 1 deletion internal/parser/gen/golang/productionstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"fmt"
"path"
"regexp"
"text/template"

"github.com/goccmack/gocc/internal/ast"
Expand All @@ -42,6 +43,8 @@ func GenProductionsTable(pkg, outDir, header string, prods ast.SyntaxProdList, s
io.WriteFile(fname, wr.Bytes())
}

var sdtTokenName = regexp.MustCompile(`\$§`)

func getProdsTab(header string, prods ast.SyntaxProdList, symbols *symbols.Symbols,
itemsets *items.ItemSets, tokMap *token.TokenMap) *prodsTabData {

Expand All @@ -61,7 +64,10 @@ func getProdsTab(header string, prods ast.SyntaxProdList, symbols *symbols.Symbo
}
switch {
case len(prod.Body.SDT) > 0:
data.ProdTab[i].ReduceFunc = fmt.Sprintf("return %s", prod.Body.SDT)
data.ProdTab[i].ReduceFunc = fmt.Sprintf(
"return %s",
sdtTokenName.ReplaceAllString(prod.Body.SDT, fmt.Sprintf("%q", prod.Id)),
)
case isEmpty:
// Empty production with no semantic action.
data.ProdTab[i].ReduceFunc = "return nil, nil"
Expand Down

0 comments on commit c1d3e66

Please sign in to comment.