Example #1
0
File: parser.go Project: h12w/gombi
func (p *parser) parseTypeName(n *parse.Node) ast.Expr {
	n = n.Child(0)
	if n.Rule() == identifier {
		return p.parseIdent(n)
	}
	return p.parseQualifiedIdent(n)
}
Example #2
0
File: parser.go Project: h12w/gombi
func (p *parser) parseForStmt(n *parse.Node) (r ast.Stmt) {
	p.openScope()
	forPos := token.Pos(n.Child(0).Pos())
	n = n.Child(1)
	if n.Is(block) {
		return &ast.ForStmt{
			For:  forPos,
			Body: p.parseBlock(n, p.topScope),
		}
	}
	option := n.Child(0).Child(0)
	body := p.parseBlock(n.Child(1), p.topScope)
	switch option.Rule() {
	case condition:
		fmt.Println(option)
	case forClause:
		fmt.Println(option)
	case rangeClause:
		forStmt := p.parseRangeStmt(option)
		forStmt.For, forStmt.Body = forPos, body
		r = forStmt
	}
	p.closeScope()
	return
}
Example #3
0
File: parser.go Project: h12w/gombi
func (p *parser) parseElement(n *parse.Node) ast.Expr {
	n = n.Child(0)
	if n.Is(value) {
		return p.parseValue(n)
	}
	return p.parseKeyValue(n)
}
Example #4
0
File: parser.go Project: h12w/gombi
func (p *parser) parseSliceType(n *parse.Node) ast.Expr {
	return &ast.ArrayType{
		Lbrack: token.Pos(n.Child(0).Pos()),
		Len:    nil,
		Elt:    p.parseType(n.Child(2)),
	}
}
Example #5
0
File: parser.go Project: h12w/gombi
func (p *parser) parsePackageClause(n *parse.Node) (token.Pos, *ast.Ident) {
	pac, name := n.Child(0), n.Child(1)
	return token.Pos(pac.Pos()), &ast.Ident{
		NamePos: token.Pos(name.Pos()),
		Name:    string(name.Value()),
	}
}
Example #6
0
File: parser.go Project: h12w/gombi
func (p *parser) parseStructType(n *parse.Node) ast.Expr {
	struct_ := n.Child(0)
	return &ast.StructType{
		Struct: token.Pos(struct_.Pos()),
		// TODO: Fields
	}
}
Example #7
0
File: parser.go Project: h12w/gombi
func (p *parser) parseLiteralValue(n *parse.Node) (exprs []ast.Expr) {
	if n.ChildCount() == 3 {
		eachListItem(element, n.Child(1), func(item *parse.Node) {
			exprs = append(exprs, p.parseElement(item))
		})
	}
	return
}
Example #8
0
File: parser.go Project: h12w/gombi
func (p *parser) parseInit(n *parse.Node) ast.Stmt {
	if n == nil {
		return nil
	}
	n = n.Child(0)
	fmt.Println(n)
	return nil
}
Example #9
0
File: parser.go Project: h12w/gombi
func (p *parser) parseImports(n *parse.Node) (decls []ast.Decl, specs []*ast.ImportSpec) {
	n.EachItem(func(item *parse.Node) {
		decl, ss := p.parseImportDecl(item.Child(0))
		decls = append(decls, decl)
		specs = append(specs, ss...)
	})
	return
}
Example #10
0
File: ua.go Project: h12w/gombi
func newProduct(productNode *parse.Node) *Product {
	return &Product{
		Name: productNode.Get(productName),
		Version: Version{
			Text: productNode.Get(productVersion),
		},
	}
}
Example #11
0
File: parser.go Project: h12w/gombi
func (p *parser) parseCompositeLit(n *parse.Node) ast.Expr {
	litValue := n.Child(1)
	return &ast.CompositeLit{
		Type:   p.parseLiteralType(n.Child(0)),
		Lbrace: token.Pos(litValue.Child(0).Pos()),
		Elts:   p.parseLiteralValue(litValue),
		Rbrace: token.Pos(litValue.LastChild().Pos()),
	}
}
Example #12
0
File: parser.go Project: h12w/gombi
func (p *parser) parseIndex(n *parse.Node) ast.Expr {
	index := n.Child(1)
	return &ast.IndexExpr{
		X:      p.parsePrimaryExpr(n.Child(0)),
		Lbrack: token.Pos(index.Child(0).Pos()),
		Index:  p.parseExpr(index.Child(1)),
		Rbrack: token.Pos(index.Child(2).Pos()),
	}
}
Example #13
0
File: parser.go Project: h12w/gombi
func (p *parser) parseValue(n *parse.Node) ast.Expr {
	n = n.Child(0)
	switch n.Rule() {
	case expr:
		return p.parseExpr(n)
	case literalValue:
	}
	fmt.Println("parseValue", n)
	return nil
}
Example #14
0
File: parser.go Project: h12w/gombi
func (p *parser) parseUnaryExpr(n *parse.Node) ast.Expr {
	if n.Child(0).Is(primaryExpr) {
		return p.parsePrimaryExpr(n.Child(0))
	}
	return &ast.UnaryExpr{
		OpPos: token.Pos(n.Child(0).Child(0).Pos()),
		Op:    token.Token(n.Child(0).Child(0).ID()),
		X:     p.parseUnaryExpr(n.Child(1)),
	}
}
Example #15
0
File: parser.go Project: h12w/gombi
func (p *parser) parseGoExpr(n *parse.Node) ast.Expr {
	n = n.Child(0).Child(0)
	switch n.Rule() {
	case expr:
		return p.parseExpr(n)
	case type_:
		return p.parseType(n)
	}
	return nil
}
Example #16
0
File: parser.go Project: h12w/gombi
func (p *parser) parseResults(n *parse.Node, scope *ast.Scope) *ast.FieldList {
	n = n.Child(0)
	switch n.Rule() {
	case parameters:
		return p.parseParams(n, scope)
	case type_:
		return &ast.FieldList{List: []*ast.Field{{Type: p.parseType(n)}}}
	}
	return nil
}
Example #17
0
File: parser.go Project: h12w/gombi
func (p *parser) parsePost(n *parse.Node) ast.Stmt {
	if n == nil {
		return nil
	}
	n = n.Child(0)
	if !n.Is(forClause) {
		return nil
	}
	fmt.Println(n)
	return nil
}
Example #18
0
File: ua.go Project: h12w/gombi
func (c Comment) append(commentNode *parse.Node) Comment {
	commentNode.Child(1).Each(func(citem *parse.Node) {
		citem = citem.Child(0)
		if citem.Is(commentText) {
			c.Items = append(c.Items, citem.Get(commentText))
		} else if citem.Is(comment) {
			c.Comments = append(c.Comments, Comment{}.append(citem))
		}
	})
	return c
}
Example #19
0
File: parser.go Project: h12w/gombi
func (p *parser) parseDecl(n *parse.Node) ast.Decl {
	n = n.Child(0)
	switch n.Rule() {
	case constDecl:
		return p.parseConstDecl(n)
	case typeDecl:
		return p.parseTypeDecl(n)
	case varDecl:
		return p.parseVarDecl(n)
	}
	return nil
}
Example #20
0
File: parser.go Project: h12w/gombi
func (p *parser) parseCallExpr(n *parse.Node) *ast.CallExpr {
	call := n.Child(1)
	callExpr := ast.CallExpr{
		Fun:    p.parsePrimaryExpr(n.Child(0)),
		Lparen: token.Pos(call.Child(0).Pos()),
		Rparen: token.Pos(call.LastChild().Pos()),
	}
	if call.ChildCount() > 2 {
		callExpr.Args, callExpr.Ellipsis = p.parseArgs(call.Child(1))
	}
	return &callExpr
}
Example #21
0
File: parser.go Project: h12w/gombi
func (p *parser) parseSwitchStmt(n *parse.Node) ast.Stmt {
	p.openScope()
	n = n.Child(0)
	switch n.Rule() {
	case exprSwitchStmt:
		return p.parseExprSwitchStmt(n)
	case typeSwitchStmt:
		return p.parseTypeSwitchStmt(n)
	}
	p.closeScope()
	return nil
}
Example #22
0
File: parser.go Project: h12w/gombi
func (p *parser) parseTopLevelDecl(n *parse.Node) ast.Decl {
	n = n.Child(0)
	switch n.Rule() {
	case decl:
		return p.parseDecl(n)
	case funcDecl:
		return p.parseFuncDecl(n)
	case methodDecl:
		return p.parseMethodDecl(n)
	}
	return nil
}
Example #23
0
File: parser.go Project: h12w/gombi
func (p *parser) parseElse(n *parse.Node) ast.Stmt {
	if n == nil {
		return nil
	}
	n = n.Child(0)
	switch n.Rule() {
	case ifStmt:
		return p.parseIfStmt(n)
	case block:
		return p.parseBlock(n, p.topScope)
	}
	return nil
}
Example #24
0
File: parser.go Project: h12w/gombi
func (p *parser) parseArgs(n *parse.Node) ([]ast.Expr, token.Pos) {
	n = n.Child(0)
	if n.Is(exprList) {
		return p.parseExprList(n), 0
	}
	return p.parseExprList(n.Child(0)), token.Pos(n.Child(1).Pos())
}
Example #25
0
File: parser.go Project: h12w/gombi
func (p *parser) parseSimpleStmt(n *parse.Node) ast.Stmt {
	n = n.Child(0)
	switch n.Rule() {
	case exprStmt:
		return p.parseExprStmt(n)
	case sendStmt:
	case incDecStmt:
	case assignment:
		return p.parseAsignment(n)
	case shortVarDecl:
		return p.parseShortVarDecl(n)
	}
	fmt.Println("simpleStmt", n)
	return nil
}
Example #26
0
File: parser.go Project: h12w/gombi
func (p *parser) parseInitCond(n *parse.Node) (ast.Stmt, ast.Expr) {
	n = n.Child(0)
	if n.Is(expr) {
		return nil, p.parseCond(n)
	} else {
		return p.parseInit(n.Child(0)), p.parseCond(n.Child(2))
	}
}
Example #27
0
File: parser.go Project: h12w/gombi
func (p *parser) parseLiteralType(n *parse.Node) ast.Expr {
	n = n.Child(0)
	switch n.Rule() {
	case arrayType:
	case structType:
		return p.parseStructType(n)
	case sliceType:
		return p.parseSliceType(n)
	case mapType:
	case typeName:
	default:
		// array
	}
	fmt.Println(n)
	return nil
}
Example #28
0
File: parser.go Project: h12w/gombi
func eachListItem(itemRule *parse.R, n *parse.Node, visit func(*parse.Node)) {
	if n.Child(0).Is(itemRule) {
		visit(n.Child(0))
		return
	}
	n.Child(0).EachItem(func(item *parse.Node) {
		visit(item.Child(0))
	})
	visit(n.Child(1))
}
Example #29
0
File: parser.go Project: h12w/gombi
func (p *parser) parseExpr(n *parse.Node) ast.Expr {
	if n == nil {
		return nil // ??
	}
	switch n.ChildCount() {
	case 1:
		return p.parseUnaryExpr(n.Child(0))
	case 3:
		op := n.Child(1).Child(0).Child(0)
		return &ast.BinaryExpr{
			X:     p.parseExpr(n.Child(0)),
			OpPos: token.Pos(op.Pos()),
			Op:    token.Token(op.ID()),
			Y:     p.parseUnaryExpr(n.Child(2)),
		}
	}
	return nil
}
Example #30
0
File: parser.go Project: h12w/gombi
func (p *parser) parseTypeLit(n *parse.Node) ast.Expr {
	n = n.Child(0)
	switch n.Rule() {
	case arrayType:
	case structType:
		return p.parseStructType(n)
	case pointerType:
		return p.parsePointerType(n)
	case funcType:
	case interfaceType:
		return p.parseInterfaceType(n)
	case sliceType:
	case mapType:
	case channelType:
	}
	fmt.Println(n)
	return nil
}