本文整理汇总了Golang中compiler.Parser类的典型用法代码示例。如果您正苦于以下问题:Golang Parser类的具体用法?Golang Parser怎么用?Golang Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Parser类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
if len(os.Args) < 3 {
fmt.Printf("usage: ./main grammar_file program_file\n")
os.Exit(1)
}
gmr, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Printf("'%s' is not a valid file name\n", os.Args[1])
}
pgm, err := ioutil.ReadFile(os.Args[2])
if err != nil {
fmt.Printf("'%s' is not a valid file name\n", os.Args[1])
}
// First file should be the grammar, second is the file being parsed
gmrReader := bytes.NewReader(gmr)
pgmReader := bytes.NewReader(pgm)
// Get the grammar from the analyzer
a := compiler.Analyzer{Reader: *gmrReader}
grammar := a.ReadGrammar()
// Create a generator, necessary for table
g := compiler.Generator{Grammar: grammar}
// Setup the parser
p := compiler.Parser{Grammar: a.ReadGrammar(), Reader: *pgmReader}
p.Scanner = compiler.Scanner{Reader: *pgmReader}
p.Table = g.GetTable()
p.Driver()
}
示例2: main
func main() {
// create a new reader and initialze a parser with it
buf, _ := ioutil.ReadFile(os.Args[1])
reader := bytes.NewReader(buf)
p := new(compiler.Parser)
p.Scanner = compiler.Scanner{Reader: *reader}
p.SystemGoal()
}
示例3: TestBlankType
func TestBlankType(t *testing.T) {
lexer := new(compiler.Lexer).Init("class A { }")
parser := new(compiler.Parser).Init(lexer)
node := parser.TypeDecl()
class := CLASS(IDENT("A"), MEMBERS())
if node.String() != class {
t.Fatalf("CLASS not parsed")
}
}
示例4: TestParsingPackage
func TestParsingPackage(t *testing.T) {
lexer := new(compiler.Lexer).Init("package a.b.c")
parser := new(compiler.Parser).Init(lexer)
node := parser.PackageDecl()
if node.Name != "PACKAGE" {
t.Fatalf("Package not parsed")
}
if node.Children[0].Name != "QNAME" {
t.Fatalf("QNAME not parsed")
}
if node.Children[0].Text != "a.b.c" {
t.Fatalf("'a.b.c' not parsed")
}
}
示例5: main
func main() {
// the file for reading
src, _ := ioutil.ReadFile(os.Args[1])
reader := bytes.NewReader(src)
// the file for writing
dst, _ := os.Create(os.Args[2])
writer := bufio.NewWriter(dst)
defer dst.Close()
// setup the parser
p := compiler.Parser{Writer: *writer}
p.Scanner = compiler.Scanner{Reader: *reader}
// parse the file!
p.SystemGoal()
}
示例6: TestTypeWithMainMethod
func TestTypeWithMainMethod(t *testing.T) {
lexer := new(compiler.Lexer).Init(
"\n" +
"class A {\n" +
" static main(args){\n" +
" }\n" +
"}\n")
parser := new(compiler.Parser).Init(lexer)
node := parser.TypeDecl()
args := ARGS(ARG(TYPE("java.lang.Object"), IDENT("args"), NIL))
mainMethod := METHOD(
MODIFIERS(STATIC),
NIL,
IDENT("main"),
args,
"METHOD_BODY")
class := CLASS(IDENT("A"), MEMBERS(mainMethod))
if node.String() != class {
t.Fatalf("CLASS not parsed")
}
}