Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc/_spec: go spec #2131

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions doc/_spec/go.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// -----------------------------------------------------------------

node Decl {
declNode()
}

node Spec {
specNode()
}

node Stmt {
stmtNode()
}

node Expr {
exprNode()
}

node GenDecl : Decl {
TokPos pos
Tok token
Lparen pos
Specs []Spec
Rparen pos
}

node ImportSpec : Spec {
Name Expr
Path BasicLit
EndPos pos
}

// -----------------------------------------------------------------

File = ("package" Name:Ident ";") Decls:(*(importDecl ";") *(toplevelDecl ";"))

toplevelDecl = valueDecl | typeDecl | FuncDecl

// -----------------------------------------------------------------

importDecl = "import" Specs:(ImportSpec | lp:"(" *(ImportSpec ";") rp:")") as GenDecl {
TokPos: import.Pos
Tok: import.Tok
Lparen: lp.Pos
Rparen: rp.Pos
}

ImportSpec = ?Name:(dotPkgName | Ident) Path:stringLit {
EndPos: Path.End
}

dotPkgName = dot:"." as Ident {
NamePos: dot.Pos
Name: "."
}

stringLit = string as BasicLit {
ValuePos: string.Pos
Kind: string.Tok
Value: string.Lit
}

// -----------------------------------------------------------------

valueDecl = valKw Specs:(ValueSpec | lp:"(" *(ValueSpec ";") rp:")") as GenDecl {
TokPos: valKw.Pos
Tok: valKw.Tok
Lparen: lp.Pos
Rparen: rp.Pos
}

valKw = "const" | "var"

ValueSpec = Names:identList ?Type ?Values:exprList

identList = Ident % ","

Ident = ident {
NamePos: ident.Pos
Name: ident.Lit
}

// -----------------------------------------------------------------

Type = typeNameEx | typeLit | "(" Type ")" as Expr

typeLit = ArrayType | StructType | pointerType | FuncType | InterfaceType | MapType | ChanType

typeNameEx = typeName ?(lp:"[" Indices:typeList rp:"]" as IndexListExpr {
X: typeName
Lbrack: lp.Pos
Rbrack: rp.Pos
})

typeName = Ident | qualifiedIdent as Expr

typeList = Type % ","

qualifiedIdent = X:Ident "." Sel:Ident as SelectorExpr

// -----------------------------------------------------------------

ArrayType = lp:"[" Len:expr "]" Elt:Type {
Lbrack: lp.Pos
}

// -----------------------------------------------------------------

AssignStmt = Lhs:exprList assignOp Rhs:exprList {
TokPos: assignOp.Pos
Tok: assignOp.Tok
}

exprList = expr % ","

assignOp = ":=" | "=" | "+=" | "-=" | "*=" | "/=" | "%="

// -----------------------------------------------------------------

expr = unaryExpr | BinaryExpr

BinaryExpr = X:expr binOp Y:expr { // TODO: precedence
OpPos: binOp.Pos
Op: binOp.Tok
}

binOp = "||" | "&&" | relOp | addOp | mulOp

relOp = "==" | "!=" | "<" | "<=" | ">" | ">="

addOp = "+" | "-" | "|" | "^"

mulOp = "*" | "/" | "%" | "<<" | ">>" | "&" | "&^"

unaryExpr = primaryExpr | (unaryOp X:unaryExpr as UnaryExpr {
OpPos: unaryOp.Pos
Op: unaryOp.Tok
})

unaryOp = "+" | "-" | "!" | "^" | "*" | "&" | "<-"

primaryExpr = operand | CallExpr | SelectorExpr | IndexExpr | SliceExpr | TypeAssertExpr

operand = TODO

CallExpr = Fun:primaryExpr lp:"(" Args:exprList ?el:"..." ?"," rp:")" {
Lparen: lp.Pos
Rparen: rp.Pos
Ellipsis: el != nil
}

SelectorExpr = X:primaryExpr "." Sel:Ident

IndexExpr = X:primaryExpr lp:"[" Index:expr ?"," rp:"]" {
Lbrack: lp.Pos
Rbrack: rp.Pos
}

SliceExpr = X:primaryExpr lp:"[" ?Low:expr ":" ?High:expr ?(":" Max:expr) rp:"]" {
Lbrack: lp.Pos
Rbrack: rp.Pos
Slice3: Max != nil
}

TypeAssertExpr = X:primaryExpr "." lp:"(" Type rp:")" {
Lparen: lp.Pos
Rparen: rp.Pos
}

// -----------------------------------------------------------------