-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
541 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("]]") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.