Пример #1
0
// Parse consumes the input string to produce an AST that represents it.
func (p *Parser) Parse(filepath, input string) (file *ast.File, err error) {
	file = &ast.File{Namespace: p.FileSet.GlobalNamespace, Name: path.Base(filepath)}
	p.file = file
	p.scope = p.FileSet.Scope
	p.namespace = p.FileSet.GlobalNamespace
	p.lexer = token.Subset(lexer.NewLexer(input), token.Significant)

	p.FileSet.Files[filepath] = p.file
	defer func() {
		if r := recover(); r != nil {
			err = append(ParseErrorList{errorf(p, "%s", r)}, p.errors...)
			if p.Debug {
				for _, err := range p.errors {
					fmt.Println(err)
				}
				panic(r)
			}
		}
	}()
	// expecting either token.HTML or token.PHPBegin
	p.file.Nodes = make([]ast.Node, 0, 1)
TokenLoop:
	for {
		p.next()
		switch p.current.Typ {
		case token.EOF:
			break TokenLoop
		default:
			n := p.parseNode()
			if n != nil {
				p.file.Nodes = append(p.file.Nodes, n)
			}
		}
	}
	if p.errors != nil {
		err = p.errors
	}
	return
}
Пример #2
0
func TestPHPLexer(t *testing.T) {
	l := token.Subset(NewLexer(testFile), token.Significant|token.CommentType)

	var i token.Item
	i = assertNext(t, l, token.PHPBegin)

	i = assertNext(t, l, token.VariableOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.AssignmentOperator)
	i = assertNext(t, l, token.StringLiteral)
	i = assertNext(t, l, token.StatementEnd)

	i = assertNext(t, l, token.VariableOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.AssignmentOperator)
	i = assertNext(t, l, token.SubtractionOperator)
	i = assertNext(t, l, token.NumberLiteral)
	i = assertNext(t, l, token.StatementEnd)

	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.OpenParen)
	i = assertNext(t, l, token.CloseParen)
	i = assertNext(t, l, token.StatementEnd)

	i = assertNext(t, l, token.CommentBlock)
	i = assertNext(t, l, token.Function)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.OpenParen)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.VariableOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.Comma)
	i = assertNext(t, l, token.VariableOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.CloseParen)
	i = assertNext(t, l, token.BlockBegin)

	i = assertNext(t, l, token.If)
	i = assertNext(t, l, token.OpenParen)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.OpenParen)
	i = assertNext(t, l, token.CloseParen)
	i = assertNext(t, l, token.CloseParen)

	i = assertNext(t, l, token.BlockBegin)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.OpenParen)
	i = assertNext(t, l, token.CloseParen)
	i = assertNext(t, l, token.StatementEnd)
	i = assertNext(t, l, token.BlockEnd)

	i = assertNext(t, l, token.BlockEnd)

	i = assertNext(t, l, token.CommentLine)
	i = assertNext(t, l, token.Class)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.BlockBegin)
	i = assertNext(t, l, token.Public)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.OpenParen)
	i = assertNext(t, l, token.CloseParen)
	i = assertNext(t, l, token.BlockBegin)
	i = assertNext(t, l, token.UnaryOperator)
	i = assertNext(t, l, token.Self)
	i = assertNext(t, l, token.ScopeResolutionOperator)
	i = assertNext(t, l, token.VariableOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.StatementEnd)
	i = assertNext(t, l, token.BlockEnd)
	i = assertNext(t, l, token.BlockEnd)

	i = assertNext(t, l, token.VariableOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.AssignmentOperator)
	i = assertNext(t, l, token.VariableOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.ObjectOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.OpenParen)
	i = assertNext(t, l, token.CloseParen)
	i = assertNext(t, l, token.StatementEnd)

	i = assertNext(t, l, token.VariableOperator)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.AssignmentOperator)
	i = assertNext(t, l, token.StringLiteral)
	i = assertNext(t, l, token.StatementEnd)

	i = assertNext(t, l, token.PHPEnd)
	i = assertNext(t, l, token.HTML)
	assertItem(t, i, "\n<html>\n")

	i = assertNext(t, l, token.PHPBegin)
	i = assertNext(t, l, token.Echo)
	i = assertNext(t, l, token.Identifier)
	i = assertNext(t, l, token.OpenParen)
	i = assertNext(t, l, token.CloseParen)
	i = assertNext(t, l, token.StatementEnd)

	i = assertNext(t, l, token.PHPEnd)
	i = assertNext(t, l, token.HTML)
	assertItem(t, i, "\n</html>")

	i = assertNext(t, l, token.EOF)
}