本文整理匯總了Golang中github.com/gbbr/textmate/vendor/quarnster/parser.Node類的典型用法代碼示例。如果您正苦於以下問題:Golang Node類的具體用法?Golang Node怎麽用?Golang Node使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Node類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Parse
func (lp *LanguageParser) Parse() (*parser.Node, error) {
sdata := string(lp.data)
rn := parser.Node{P: lp, Name: lp.l.ScopeName}
defer func() {
if r := recover(); r != nil {
log.Printf("Panic during parse: %v\n", r)
log.Printf("%v", rn)
}
}()
iter := maxiter
for i := 0; i < len(sdata) && iter > 0; iter-- {
pat, ret := lp.l.RootPattern.Cache(sdata, i)
nl := strings.IndexAny(sdata[i:], "\n\r")
if nl != -1 {
nl += i
}
if ret == nil {
break
} else if nl > 0 && nl <= ret[0] {
i = nl
for i < len(sdata) && (sdata[i] == '\n' || sdata[i] == '\r') {
i++
}
} else {
n := pat.CreateNode(sdata, i, lp, ret)
rn.Append(n)
i = n.Range.B
}
}
rn.UpdateRange()
if len(sdata) != 0 {
lut := make([]int, len(sdata)+1)
j := 0
for i := range sdata {
lut[i] = j
j++
}
lut[len(sdata)] = len(lp.data)
lp.patch(lut, &rn)
}
if iter == 0 {
panic("reached maximum number of iterations")
}
return &rn, nil
}
示例2: CreateCaptureNodes
func (p *Pattern) CreateCaptureNodes(data string, pos int, d parser.DataSource, mo MatchObject, parent *parser.Node, capt Captures) {
ranges := make([]text.Region, len(mo)/2)
parentIndex := make([]int, len(ranges))
parents := make([]*parser.Node, len(parentIndex))
for i := range ranges {
ranges[i] = text.Region{A: mo[i*2+0], B: mo[i*2+1]}
if i < 2 {
parents[i] = parent
continue
}
r := ranges[i]
for j := i - 1; j >= 0; j-- {
if ranges[j].Covers(r) {
parentIndex[i] = j
break
}
}
}
for _, v := range capt {
i := v.Key
if i >= len(parents) || ranges[i].A == -1 {
continue
}
child := &parser.Node{Name: v.Name, Range: ranges[i], P: d}
parents[i] = child
if i == 0 {
parent.Append(child)
continue
}
var p *parser.Node
for p == nil {
i = parentIndex[i]
p = parents[i]
}
p.Append(child)
}
}
示例3: Recurse
func (c *Compiler) Recurse(node *parser.Node) string {
ret := ""
var cf parser.CodeFormatter
p := primitives[node.Name]
if p != "" {
return p
}
switch node.Name {
case "Identifier", "Boolean", "Float", "Integer":
return node.Data()
case "Text":
return "\"" + node.Data() + "\""
case "Comparison":
a := c.Recurse(node.Children[0])
cmp := c.Recurse(node.Children[1])
b := c.Recurse(node.Children[2])
return a + cmp + b
case "While":
return "while (" + c.Recurse(node.Children[0]) + ")\n" + c.Recurse(node.Children[1])
case "If":
return "if (" + c.Recurse(node.Children[0]) + ")\n" + c.Recurse(node.Children[1])
case "ElseIf":
ret += "else "
case "Else":
ret += "else\n"
case "For":
variable := c.Recurse(node.Children[0])
array := c.Recurse(node.Children[1])
block := c.Recurse(node.Children[2])
typeName := "TODO"
ret := "for (int i = 0; i < " + array + ".size(); i++)\n{\n\t"
ret += typeName + " " + variable + " = " + array + "[i];\n"
ret += block[2:]
return ret
case "NewStatement":
return "new " + c.Recurse(node.Children[0])
case "ReturnStatement":
return "return " + c.Recurse(node.Children[0])
case "ArrayIndexing":
return c.Recurse(node.Children[0]) + "[" + c.Recurse(node.Children[1]) + "]"
case "ArraySlicing":
id := c.Recurse(node.Children[0])
a := "0"
b := id + ".size()"
if node.Children[1].Name != "colon" {
a = c.Recurse(node.Children[1])
}
back := node.Children[len(node.Children)-1]
if back.Name != "colon" {
b = c.Recurse(back)
}
return id + ".slice(" + a + ", " + b + ")"
case "PostInc":
return c.Recurse(node.Children[0]) + "++"
case "PostDec":
return c.Recurse(node.Children[0]) + "--"
case "FunctionCall":
ret = node.Children[0].Data()
args := ""
for _, child := range node.Children[1:] {
if args != "" {
args += ", "
}
args += c.Recurse(child)
}
return ret + "(" + args + ")"
case "Class":
cn := node.Children[0].Data()
c.currentClass = cn
cf.Add("class " + cn + "\n{\npublic:\n")
cf.Inc()
for _, child := range node.Children[1:] {
ret := c.Recurse(child)
if child.Name == "VariableDeclaration" {
ret += ";\n"
}
cf.Add(ret)
}
cf.Dec()
cf.Add("};\n")
c.currentClass = ""
return cf.String()
case "Assignment":
return node.Children[0].Data() + " = " + c.Recurse(node.Children[1])
case "PlusEquals":
return node.Children[0].Data() + " += " + c.Recurse(node.Children[1])
case "VariableDeclaration":
ret := c.ResolveType(node.Children[0]) + " "
n1 := node.Children[1]
if n1.Name == "Assignment" {
ret += c.Recurse(n1)
} else {
ret += n1.Data()
}
return ret
case "Block":
//.........這裏部分代碼省略.........
示例4: ResolveType
func (c *Compiler) ResolveType(node *parser.Node) string {
if node.Name != "Type" {
panic(node)
}
return node.Data()
}
示例5: Recurse
func (c *PyCompiler) Recurse(node *parser.Node) string {
ret := ""
var cf parser.CodeFormatter
p := pyprimitives[node.Name]
if p != "" {
return p
}
switch node.Name {
case "Identifier", "Float", "Integer":
return node.Data()
case "Boolean":
if node.Data() == "true" {
return "True"
} else {
return "False"
}
case "Text":
return "\"" + node.Data() + "\""
case "Comparison":
a := c.Recurse(node.Children[0])
cmp := c.Recurse(node.Children[1])
b := c.Recurse(node.Children[2])
return a + cmp + b
case "While":
return "while " + c.Recurse(node.Children[0]) + ":\n" + c.Recurse(node.Children[1])
case "If":
return "if " + c.Recurse(node.Children[0]) + ":\n" + c.Recurse(node.Children[1])
case "ElseIf":
return "el" + c.Recurse(node.Children[0])
case "Else":
return "else:" + c.Recurse(node.Children[0])
case "For":
variable := c.Recurse(node.Children[0])
array := c.Recurse(node.Children[1])
block := c.Recurse(node.Children[2])
return "for " + variable + " in " + array + ":\n" + block
case "NewStatement":
return c.Recurse(node.Children[0]) + "()"
case "ReturnStatement":
return "return " + c.Recurse(node.Children[0])
case "ArrayIndexing":
return c.Recurse(node.Children[0]) + "[" + c.Recurse(node.Children[1]) + "]"
case "ArraySlicing":
id := c.Recurse(node.Children[0])
a := ""
b := ""
if node.Children[1].Name != "colon" {
a = c.Recurse(node.Children[1])
}
back := node.Children[len(node.Children)-1]
if back.Name != "colon" {
b = c.Recurse(back)
}
return id + "[" + a + ":" + b + "]"
case "PostInc":
return c.Recurse(node.Children[0]) + " += 1"
case "PostDec":
return c.Recurse(node.Children[0]) + " -= 1"
case "FunctionCall":
ret = node.Children[0].Data()
args := ""
for _, child := range node.Children[1:] {
if args != "" {
args += ", "
}
args += c.Recurse(child)
}
return ret + "(" + args + ")"
case "Class":
cn := node.Children[0].Data()
c.currentClass = cn
cf.Add("class " + cn + ":\n")
cf.Inc()
for _, child := range node.Children[1:] {
ret := c.Recurse(child)
if child.Name == "VariableDeclaration" {
ret += "\n"
}
cf.Add(ret)
}
cf.Dec()
cf.Add("\n")
c.currentClass = ""
return cf.String()
case "Assignment":
return node.Children[0].Data() + " = " + c.Recurse(node.Children[1])
case "PlusEquals":
return node.Children[0].Data() + " += " + c.Recurse(node.Children[1])
case "VariableDeclaration":
ret := "" //c.ResolveType(node.Children[0]) + " "
n1 := node.Children[1]
if n1.Name == "Assignment" {
ret += c.Recurse(n1)
} else {
ret += n1.Data()
}
//.........這裏部分代碼省略.........