Lexical and syntactic analysis of go language (2) – Analysis of import declaration

Time:2021-4-17

Last articleLexical and grammatical analysis of go language (1)The author describes the process of gradual lexical analysis, through which the source file has been transformed into Syntax.File The properties of the structure are as follows:

Lexical and syntactic analysis of go language (2) - Analysis of import declaration

The pkgname property in the file structure has been initialized tosyntax.Name{Value: “main”}. After the go language scanner processes the package name pkgname, the import token will be scanned in the next parsing. At this time, the B, R, e attributes of * scanner are as follows:

Lexical and syntactic analysis of go language (2) - Analysis of import declaration

Again, bybuf[b:r]The token of the current scan is calculated as the import string. After comparing the go keyword list, the tok is set to built-in_ In this case, the internal attributes of * scanner are as follows:

Lexical and syntactic analysis of go language (2) - Analysis of import declaration

Then the scanner will startloopParse the import declaration of the source file until the token scanned is not equal to_ Import, and save the path part of the import (such as net / HTTP, main) to the decllist array (slice) of the scanner.

for p.got(_Import) {
    f.DeclList = p.appendGroup(f.DeclList, p.importDecl)
}

func (p *parser) got(tok token) bool {
    if p.tok == tok {
        p.next()
        return true
    }
    return false
}

There are many different forms of declaration for import. The common grammars are as follows:

import . path/package
import alias path/package
import path/package
import _ path/package

// by group
import (
    . path/package
    alias path/package
    path/package
    _ path/package
)

In go language, importdecl structure is used to describe import declaration

ImportDecl struct {
    Group * group // group, the group under the same bracket is the same group
    Localpkgname * name // alias
    Path * basiclit // path, such as net / HTTP
}

When parsing import morphology, go language will try to initialize the importdecl structure and assign values to its attributes. The following author will view the content of the assigned importdecl structure according to different situations.

  • Suppose the statement isimport "net/http"After parsing the statement, the structure content is as follows:

Lexical and syntactic analysis of go language (2) - Analysis of import declaration

The pkgname property (package alias) in the structure is set tosyntax.Name{Value: ""}, the group is set to nil, and the path part is set tosyntax.BasicLit{Value: "net/http"}

  • Suppose the statement isimport alias "net/http"After parsing the statement, the structure content is as follows:

Lexical and syntactic analysis of go language (2) - Analysis of import declaration

  • Suppose the statement isimport . "net/http"After parsing the statement, the structure content is as follows:

Lexical and syntactic analysis of go language (2) - Analysis of import declaration

  • Suppose the declarative statements are grouped as follows
import (
    "fmt"
    "net/http"
)

After parsing the statement, the structure content is as follows (multiple * importdecl point to the same group)

Lexical and syntactic analysis of go language (2) - Analysis of import declaration

At this point, the import syntax declaration has been basically parsed, and then go language will parse itconst, val, func, typeetc.TopLevelDeclAfter all, the top-level declaration of go language only has these six keywords. After all, the lexical and grammatical analysis of go language is completed.

package
import
const
var
type
func

For the last articleLexical and grammatical analysis of go language (1)In main.go After this step of parsing, syntax.File The structure information is as follows:

Lexical and syntactic analysis of go language (2) - Analysis of import declaration

The following is the code of other topleveldecl functions processed by go language after processing the import declarationsrc/cmd/compile/internal/syntax/parser.go:399

// { ImportDecl ";" }
for p.got(_Import) {
    f.DeclList = p.appendGroup(f.DeclList, p.importDecl)
}

// { TopLevelDecl ";" }
for p.tok != _EOF {
    switch p.tok {
    case _Const:
        //Processing constants
    case _Type:
        //Processing type definition
    case _Var:
        //Processing variables
    case _Func:
        //Processing function
    default:
        p.syntaxError("non-declaration statement outside function body")
    }
}

It can also be seen that the import declaration can only be placed after the package declaration and in front of other top-level declarations, because after the parsing of the import declaration is completed, the scanner will go to the next for loop to continue parsing other topleveldecl, if it encounters_ Import token, the program will go to the default branch of switch, resulting in compilation errors.

./prog.go:11:1: syntax error: non-declaration statement outside function body

The article is posted on the author’s blogTwo Leng’s chatting fishWelcome to watchGo language compilation principle — Analysis of import declaration

Next read

In the next article, we will discuss the parsing of constants in go language, and the author will continue to output this part in later articles.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Two Leng’s chatting fish

Recommended Today

Envoy announced alpha version of native support for windows

Author: sunjay Bhatia Since 2016, porting envoy to the windows platform has been an important part of the projectOne of the goalsToday, we are excited to announce the alpha version of envoy’s windows native support. The contributor community has been working hard to bring the rich features of envoy to windows, which is another step […]