Skip to content

Commit

Permalink
Merge branch 'v1.2.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuvist committed Nov 14, 2019
2 parents 9c63122 + 5cef956 commit cdef90a
Show file tree
Hide file tree
Showing 55 changed files with 541 additions and 231 deletions.
10 changes: 4 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func usage() {
func main() {
flag.Usage = usage
isDebug := flag.Bool("debug", false, "use debug mode")
noLine := flag.Bool("noline", false, "skip line number hint in generated code")
version := flag.Bool("version", false, "show gorazor version info")
quick := flag.Bool("q", false, "enable quick mode; skip template render optimzation")
namespacePrefix := flag.String("prefix", "", "tpl namespace prefix")
Expand All @@ -31,12 +32,9 @@ func main() {

options := razorcore.Option{}

if *isDebug {
options["Debug"] = *isDebug
}
if *nameNotChange {
options["NameNotChange"] = *nameNotChange
}
options.IsDebug = *isDebug
options.NameNotChange = *nameNotChange
options.NoLineNumber = *noLine

if len(flag.Args()) != 2 {
flag.Usage()
Expand Down
140 changes: 140 additions & 0 deletions pkg/razorcore/ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package razorcore

import "fmt"

// Ast stores the abstract syntax tree
type Ast struct {
Parent *Ast
Children []interface{}
Mode int
TagName string
}

// ModeStr return string representation of ast mode
func (ast *Ast) ModeStr() string {
switch ast.Mode {
case PRG:
return "PROGRAM"
case MKP:
return "MARKUP"
case BLK:
return "BLOCK"
case EXP:
return "EXP"
default:
return "UNDEF"
}
}

func (ast *Ast) check() {
if len(ast.Children) >= 100000 {
panic("Maximum number of elements exceeded.")
}
}

func (ast *Ast) addChild(child interface{}) {
ast.Children = append(ast.Children, child)
ast.check()
if _a, ok := child.(*Ast); ok {
_a.Parent = ast
}
}

func (ast *Ast) addChildren(children []Token) {
for _, c := range children {
ast.addChild(c)
}
}

func (ast *Ast) addAst(_ast *Ast) {
c := _ast
for {
if len(c.Children) != 1 {
break
}
first := c.Children[0]
if _, ok := first.(*Ast); !ok {
break
}
c = first.(*Ast)
}
if c.Mode != PRG {
ast.addChild(c)
} else {
for _, x := range c.Children {
ast.addChild(x)
}
}
}

func (ast *Ast) popChild() {
l := len(ast.Children)
if l == 0 {
return
}
ast.Children = ast.Children[:l-1]
}

func (ast *Ast) beget(mode int, tag string) *Ast {
child := &Ast{nil, []interface{}{}, mode, tag}
ast.addChild(child)
return child
}

func (ast *Ast) closest(mode int, tag string) *Ast {
p := ast
for {
if p.TagName != tag && p.Parent != nil {
p = p.Parent
} else {
break
}
}
return p
}

func (ast *Ast) hasNonExp() bool {
if ast.Mode != EXP {
return true
}

for _, c := range ast.Children {
if v, ok := c.(*Ast); ok {
if v.hasNonExp() {
return true
}
}
return false
}

return false
}

func (ast *Ast) debug(depth int, max int) {
if depth >= max {
return
}
for i := 0; i < depth; i++ {
fmt.Printf("%c", '-')
}
fmt.Printf("TagName: %s Mode: %s Children: %d [[ \n", ast.TagName, ast.ModeStr(), len(ast.Children))
for _, a := range ast.Children {
if _, ok := a.(*Ast); ok {
b := (*Ast)(a.(*Ast))
b.debug(depth+1, max)
} else {
if depth+1 < max {
aa := (Token)(a.(Token))
for i := 0; i < depth+1; i++ {
fmt.Printf("%c", '-')
}
aa.P()
}
}
}
for i := 0; i < depth; i++ {
fmt.Printf("%c", '-')
}

fmt.Println("]]")
}
54 changes: 39 additions & 15 deletions pkg/razorcore/gogen.go → pkg/razorcore/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
)

Expand Down Expand Up @@ -54,6 +55,7 @@ func getValStr(e interface{}) string {
type Part struct {
ptype int
value string
line int
}

// Compiler generate go code for gorazor template
Expand Down Expand Up @@ -101,8 +103,8 @@ func (cp *Compiler) isLayoutSectionPart(p Part) (is bool, val string) {
}

val = p.value[21 : len(p.value)-3]
for _, p := range cp.paramNames {
if val == p {
for _, name := range cp.paramNames {
if val == name {
return true, val
}
}
Expand Down Expand Up @@ -130,6 +132,13 @@ func (cp *Compiler) isLayoutSectionTest(p Part) (is bool, val string) {
return
}

func (cp *Compiler) getLineHint(line int) string {
if cp.options.NoLineNumber {
return ""
}
return "// Line: " + strconv.Itoa(line) + "\n"
}

func (cp *Compiler) genPart() {
res := ""

Expand All @@ -141,6 +150,10 @@ func (cp *Compiler) genPart() {
}
if p.value != "" {
p.value = fmt.Sprintf("%#v", p.value)
if p.line > 0 {
res += cp.getLineHint(p.line)
}

res += "_buffer.WriteString(" + p.value + ")\n"
}
} else if p.ptype == CBLK {
Expand All @@ -150,6 +163,7 @@ func (cp *Compiler) genPart() {
res += p.value + "\n"
}
} else if ok, val := cp.isLayoutSectionPart(p); ok {
res += cp.getLineHint(p.line)
res += val + "(_buffer)\n"
} else {
res += p.value
Expand All @@ -161,7 +175,7 @@ func (cp *Compiler) genPart() {
func makeCompiler(ast *Ast, options Option, input string) *Compiler {
dir := filepath.Base(filepath.Dir(input))
file := strings.Replace(filepath.Base(input), gzExtension, "", 1)
if options["NameNotChange"] == nil {
if !options.NameNotChange {
file = Capitalize(file)
}
cp := &Compiler{
Expand All @@ -184,12 +198,12 @@ func makeCompiler(ast *Ast, options Option, input string) *Compiler {
return cp
}

func (cp *Compiler) visitBLK(child interface{}, ast *Ast) {
cp.addPart(Part{CBLK, getValStr(child)})
func (cp *Compiler) visitBLK(child Token) {
cp.addPart(Part{CBLK, getValStr(child), child.Line})
}

func (cp *Compiler) visitMKP(child interface{}, ast *Ast) {
cp.addPart(Part{CMKP, getValStr(child)})
func (cp *Compiler) visitMKP(child Token) {
cp.addPart(Part{CMKP, getValStr(child), child.Line})
}

func (cp *Compiler) settleLayout(layoutFunc string) {
Expand Down Expand Up @@ -325,17 +339,27 @@ func (cp *Compiler) visitExp(child interface{}, parent *Ast, idx int, isHomo boo
end += ")"
}

lineHint := ""
lineNumber := 0
if ppNotExp && idx == 0 {
if token, ok := child.(Token); ok {
lineNumber = token.Line
lineHint = cp.getLineHint(token.Line)
}
start = "_buffer.WriteString(" + start
}
if ppNotExp && idx == ppChildCnt-1 {
end += ")\n"
}

if val == "raw" {
cp.addPart(Part{CSTAT, start + end})
cp.addPart(Part{CSTAT, lineHint + start + end, lineNumber})
} else {
cp.addPart(Part{CSTAT, start + val + end})
p := Part{CSTAT, start + val + end, lineNumber}
if ok, _ := cp.isLayoutSectionPart(p); !ok {
p.value = lineHint + p.value
}
cp.addPart(p)
}
}

Expand All @@ -358,8 +382,8 @@ func (cp *Compiler) visitAstBlk(ast *Ast) {
if remove && (idx == 0 || idx == len(ast.Children)-1) {
continue
}
if _, ok := c.(Token); ok {
cp.visitBLK(c, ast)
if token, ok := c.(Token); ok {
cp.visitBLK(token)
} else {
cp.visitAst(c.(*Ast))
}
Expand All @@ -372,8 +396,8 @@ func (cp *Compiler) visitAst(ast *Ast) {
case MKP:
cp.firstBLK = 1
for _, c := range ast.Children {
if _, ok := c.(Token); ok {
cp.visitMKP(c, ast)
if token, ok := c.(Token); ok {
cp.visitMKP(token)
} else {
cp.visitAst(c.(*Ast))
}
Expand Down Expand Up @@ -594,7 +618,7 @@ func run(path string, Options Option) (*Compiler, error) {
}

//DEBUG
if Options["Debug"] != nil {
if Options.IsDebug {
fmt.Println("------------------- TOKEN START -----------------")
for _, elem := range res {
elem.P()
Expand All @@ -610,7 +634,7 @@ func run(path string, Options Option) (*Compiler, error) {
}

//DEBUG
if Options["Debug"] != nil {
if Options.IsDebug {
fmt.Println("--------------------- AST START -----------------")
parser.ast.debug(0, 20)
fmt.Println("--------------------- AST END -----------------")
Expand Down
2 changes: 1 addition & 1 deletion pkg/razorcore/gorazor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestDebug(t *testing.T) {
casedir, _ := filepath.Abs(filepath.Dir("./cases/"))
outdir, _ := filepath.Abs(filepath.Dir("./" + testdata + "/"))
option := Option{}
option["Debug"] = true
option.IsDebug = true
GenFile(casedir+"/var.gohtml", outdir+"/_var.gohtml", option)
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/razorcore/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ var typeStr = [...]string{
"TEXT_TAG_OPEN", "COMMENT_TAG_OPEN", "COMMENT_TAG_CLOSE", "WHITESPACE"}

// Option have following options:
// Debug bool
// Watch bool
// NameNotChange bool
type Option map[string]interface{}
type Option struct {
IsDebug bool
NoLineNumber bool
NameNotChange bool
}

// TokenMatch store matched token
type TokenMatch struct {
Expand Down Expand Up @@ -194,9 +195,8 @@ func tryPeekNext(text string) (match string, tokVal int, ok bool) {
func (lexer *Lexer) Scan() ([]Token, error) {
toks := []Token{}
text := strings.Replace(lexer.Text, "\r\n", "\n", -1)
text = strings.Replace(text, "\r", "\n", -1)
text += "\n"
cur, line, pos := 0, 0, 0
cur, line, pos := 0, 1, 0
for cur < len(text) {
val, left := text[cur], text[cur:]
var tok Token
Expand Down
Loading

0 comments on commit cdef90a

Please sign in to comment.