Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
0cee3028
Commit
0cee3028
authored
Jul 15, 2008
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- func reorg to reduce forward decls and improve structure
SVN=127229
parent
d9d5f3b3
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
372 additions
and
364 deletions
+372
-364
usr/gri/gosrc/parser.go
usr/gri/gosrc/parser.go
+372
-364
No files found.
usr/gri/gosrc/parser.go
View file @
0cee3028
...
@@ -127,9 +127,13 @@ func (P *Parser) Declare(obj *Globals.Object) {
...
@@ -127,9 +127,13 @@ func (P *Parser) Declare(obj *Globals.Object) {
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Common productions
func
(
P
*
Parser
)
TryType
()
bool
;
func
(
P
*
Parser
)
TryType
()
bool
;
func
(
P
*
Parser
)
ParseExpression
();
func
(
P
*
Parser
)
ParseExpression
();
func
(
P
*
Parser
)
TryStatement
()
bool
;
func
(
P
*
Parser
)
ParseDeclaration
();
func
(
P
*
Parser
)
ParseIdent
()
{
func
(
P
*
Parser
)
ParseIdent
()
{
...
@@ -163,12 +167,8 @@ func (P *Parser) ParseQualifiedIdent() {
...
@@ -163,12 +167,8 @@ func (P *Parser) ParseQualifiedIdent() {
}
}
func
(
P
*
Parser
)
ParseTypeName
()
{
// ----------------------------------------------------------------------------
P
.
Trace
(
"TypeName"
);
// Types
P
.
ParseQualifiedIdent
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseType
()
{
func
(
P
*
Parser
)
ParseType
()
{
P
.
Trace
(
"Type"
);
P
.
Trace
(
"Type"
);
...
@@ -179,6 +179,13 @@ func (P *Parser) ParseType() {
...
@@ -179,6 +179,13 @@ func (P *Parser) ParseType() {
}
}
func
(
P
*
Parser
)
ParseTypeName
()
{
P
.
Trace
(
"TypeName"
);
P
.
ParseQualifiedIdent
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseArrayType
()
{
func
(
P
*
Parser
)
ParseArrayType
()
{
P
.
Trace
(
"ArrayType"
);
P
.
Trace
(
"ArrayType"
);
P
.
Expect
(
Scanner
.
LBRACK
);
P
.
Expect
(
Scanner
.
LBRACK
);
...
@@ -204,8 +211,103 @@ func (P *Parser) ParseChannelType() {
...
@@ -204,8 +211,103 @@ func (P *Parser) ParseChannelType() {
}
}
func
(
P
*
Parser
)
ParseParameters
();
func
(
P
*
Parser
)
ParseParameterSection
()
{
func
(
P
*
Parser
)
TryResult
()
bool
;
P
.
Trace
(
"ParameterSection"
);
P
.
ParseIdentList
();
P
.
ParseType
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseParameterList
()
{
P
.
Trace
(
"ParameterList"
);
P
.
ParseParameterSection
();
for
P
.
tok
==
Scanner
.
COMMA
{
P
.
Next
();
P
.
ParseParameterSection
();
}
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseParameters
()
{
P
.
Trace
(
"Parameters"
);
P
.
Expect
(
Scanner
.
LPAREN
);
if
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
ParseParameterList
();
}
P
.
Expect
(
Scanner
.
RPAREN
);
P
.
Ecart
();
}
func
(
P
*
Parser
)
TryResult
()
bool
{
P
.
Trace
(
"Result (try)"
);
res
:=
false
;
if
P
.
tok
==
Scanner
.
LPAREN
{
// TODO: here we allow empty returns - should proably fix this
P
.
ParseParameters
();
res
=
true
;
}
else
{
res
=
P
.
TryType
();
}
P
.
Ecart
();
return
res
;
}
// Anonymous signatures
//
// (params)
// (params) type
// (params) (results)
// (recv) . (params)
// (recv) . (params) type
// (recv) . (params) (results)
func
(
P
*
Parser
)
ParseAnonymousSignature
()
{
P
.
Trace
(
"AnonymousSignature"
);
P
.
OpenScope
();
P
.
ParseParameters
();
if
P
.
tok
==
Scanner
.
PERIOD
{
P
.
Next
();
P
.
ParseParameters
();
}
P
.
TryResult
();
P
.
CloseScope
();
P
.
Ecart
();
}
// Named signatures
//
// name (params)
// name (params) type
// name (params) (results)
// (recv) name (params)
// (recv) name (params) type
// (recv) name (params) (results)
func
(
P
*
Parser
)
ParseNamedSignature
()
{
P
.
Trace
(
"NamedSignature"
);
P
.
OpenScope
();
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
ParseParameters
();
}
P
.
ParseIdent
();
// function name
P
.
ParseParameters
();
P
.
TryResult
();
P
.
CloseScope
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseFunctionType
()
{
P
.
Trace
(
"FunctionType"
);
P
.
Expect
(
Scanner
.
FUNC
);
P
.
ParseAnonymousSignature
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseMethodDecl
()
{
func
(
P
*
Parser
)
ParseMethodDecl
()
{
...
@@ -232,17 +334,6 @@ func (P *Parser) ParseInterfaceType() {
...
@@ -232,17 +334,6 @@ func (P *Parser) ParseInterfaceType() {
}
}
func
(
P
*
Parser
)
ParseAnonymousSignature
();
func
(
P
*
Parser
)
ParseFunctionType
()
{
P
.
Trace
(
"FunctionType"
);
P
.
Expect
(
Scanner
.
FUNC
);
P
.
ParseAnonymousSignature
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseMapType
()
{
func
(
P
*
Parser
)
ParseMapType
()
{
P
.
Trace
(
"MapType"
);
P
.
Trace
(
"MapType"
);
P
.
Expect
(
Scanner
.
MAP
);
P
.
Expect
(
Scanner
.
MAP
);
...
@@ -316,35 +407,44 @@ func (P *Parser) TryType() bool {
...
@@ -316,35 +407,44 @@ func (P *Parser) TryType() bool {
}
}
func
(
P
*
Parser
)
ParseImportSpec
()
{
// ----------------------------------------------------------------------------
P
.
Trace
(
"ImportSpec"
);
// Blocks
if
P
.
tok
==
Scanner
.
PERIOD
{
P
.
Next
();
func
(
P
*
Parser
)
ParseStatement
()
{
}
else
if
P
.
tok
==
Scanner
.
IDENT
{
P
.
Trace
(
"Statement"
);
P
.
Next
();
if
!
P
.
TryStatement
()
{
P
.
Error
(
P
.
beg
,
"statement expected"
);
}
}
P
.
Expect
(
Scanner
.
STRING
);
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseImportDecl
()
{
func
(
P
*
Parser
)
ParseStatementList
()
{
P
.
Trace
(
"ImportDecl"
);
P
.
Trace
(
"StatementList"
);
P
.
Expect
(
Scanner
.
IMPORT
);
for
P
.
TryStatement
()
{
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
Optional
(
Scanner
.
SEMICOLON
);
P
.
Next
();
for
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
ParseImportSpec
();
P
.
Optional
(
Scanner
.
SEMICOLON
);
// TODO this seems wrong
}
}
P
.
Next
();
P
.
Ecart
();
}
else
{
}
P
.
ParseImportSpec
();
func
(
P
*
Parser
)
ParseBlock
()
{
P
.
Trace
(
"Block"
);
P
.
Expect
(
Scanner
.
LBRACE
);
P
.
OpenScope
();
if
P
.
tok
!=
Scanner
.
RBRACE
&&
P
.
tok
!=
Scanner
.
SEMICOLON
{
P
.
ParseStatementList
();
}
}
P
.
Optional
(
Scanner
.
SEMICOLON
);
P
.
CloseScope
();
P
.
Expect
(
Scanner
.
RBRACE
);
P
.
Ecart
();
P
.
Ecart
();
}
}
// ----------------------------------------------------------------------------
// Expressions
func
(
P
*
Parser
)
ParseExpressionList
()
{
func
(
P
*
Parser
)
ParseExpressionList
()
{
P
.
Trace
(
"ExpressionList"
);
P
.
Trace
(
"ExpressionList"
);
P
.
ParseExpression
();
P
.
ParseExpression
();
...
@@ -356,209 +456,190 @@ func (P *Parser) ParseExpressionList() {
...
@@ -356,209 +456,190 @@ func (P *Parser) ParseExpressionList() {
}
}
func
(
P
*
Parser
)
ParseConstSpec
()
{
func
(
P
*
Parser
)
ParseNew
()
{
P
.
Trace
(
"ConstSpec"
);
P
.
Trace
(
"New"
);
P
.
ParseIdent
();
P
.
Expect
(
Scanner
.
NEW
);
P
.
TryType
();
P
.
Expect
(
Scanner
.
LPAREN
);
if
P
.
tok
==
Scanner
.
ASSIGN
{
P
.
ParseType
();
if
P
.
tok
==
Scanner
.
COMMA
{
P
.
Next
();
P
.
Next
();
P
.
ParseExpression
();
P
.
ParseExpression
List
()
}
}
P
.
Expect
(
Scanner
.
RPAREN
);
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseConstDecl
()
{
func
(
P
*
Parser
)
ParseFunctionLit
()
{
P
.
Trace
(
"ConstDecl"
);
P
.
Trace
(
"FunctionLit"
);
P
.
Expect
(
Scanner
.
CONST
);
P
.
ParseFunctionType
();
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
ParseBlock
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseOperand
()
{
P
.
Trace
(
"Operand"
);
switch
P
.
tok
{
case
Scanner
.
IDENT
:
P
.
ParseQualifiedIdent
();
case
Scanner
.
LPAREN
:
P
.
Next
();
P
.
Next
();
for
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
ParseExpression
();
P
.
ParseConstSpec
();
P
.
Expect
(
Scanner
.
RPAREN
);
if
P
.
tok
!=
Scanner
.
RPAREN
{
case
Scanner
.
STRING
:
fallthrough
;
P
.
Expect
(
Scanner
.
SEMICOLON
);
case
Scanner
.
NUMBER
:
fallthrough
;
}
case
Scanner
.
NIL
:
fallthrough
;
}
case
Scanner
.
IOTA
:
fallthrough
;
case
Scanner
.
TRUE
:
fallthrough
;
case
Scanner
.
FALSE
:
P
.
Next
();
P
.
Next
();
}
else
{
case
Scanner
.
FUNC
:
P
.
ParseConstSpec
();
P
.
ParseFunctionLit
();
case
Scanner
.
NEW
:
P
.
ParseNew
();
default
:
P
.
Error
(
P
.
beg
,
"operand expected"
);
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseTypeSpec
()
{
func
(
P
*
Parser
)
ParseSelectorOrTypeAssertion
()
{
P
.
Trace
(
"TypeSpec"
);
P
.
Trace
(
"SelectorOrTypeAssertion"
);
P
.
Expect
(
Scanner
.
PERIOD
);
if
P
.
tok
==
Scanner
.
IDENT
{
P
.
ParseIdent
();
P
.
ParseIdent
();
P
.
TryType
();
}
else
{
P
.
Expect
(
Scanner
.
LPAREN
);
P
.
ParseType
();
P
.
Expect
(
Scanner
.
RPAREN
);
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseTypeDecl
()
{
func
(
P
*
Parser
)
ParseIndexOrSlice
()
{
P
.
Trace
(
"TypeDecl"
);
P
.
Trace
(
"IndexOrSlice"
);
P
.
Expect
(
Scanner
.
TYPE
);
P
.
Expect
(
Scanner
.
LBRACK
);
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
ParseExpression
();
P
.
Next
();
if
P
.
tok
==
Scanner
.
COLON
{
for
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
ParseTypeSpec
();
if
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
Expect
(
Scanner
.
SEMICOLON
);
}
}
P
.
Next
();
P
.
Next
();
}
else
{
P
.
ParseExpression
();
P
.
ParseTypeSpec
();
}
}
P
.
Expect
(
Scanner
.
RBRACK
);
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseVarSpec
()
{
func
(
P
*
Parser
)
ParseInvocation
()
{
P
.
Trace
(
"VarSpec"
);
P
.
Trace
(
"Invocation"
);
P
.
ParseIdentList
();
P
.
Expect
(
Scanner
.
LPAREN
);
if
P
.
tok
==
Scanner
.
ASSIGN
{
if
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
Next
();
P
.
ParseExpressionList
();
}
else
{
P
.
ParseType
();
if
P
.
tok
==
Scanner
.
ASSIGN
{
P
.
Next
();
P
.
ParseExpressionList
();
P
.
ParseExpressionList
();
}
}
}
P
.
Expect
(
Scanner
.
RPAREN
);
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseVarDecl
()
{
func
(
P
*
Parser
)
ParsePrimaryExpr
()
{
P
.
Trace
(
"VarDecl"
);
P
.
Trace
(
"PrimaryExpr"
);
P
.
Expect
(
Scanner
.
VAR
);
P
.
ParseOperand
();
if
P
.
tok
==
Scanner
.
LPAREN
{
for
{
P
.
Next
();
switch
P
.
tok
{
for
P
.
tok
!=
Scanner
.
RPAREN
{
case
Scanner
.
PERIOD
:
P
.
ParseVarSpec
();
P
.
ParseSelectorOrTypeAssertion
();
if
P
.
tok
!=
Scanner
.
RPAREN
{
case
Scanner
.
LBRACK
:
P
.
Expect
(
Scanner
.
SEMICOLON
);
P
.
ParseIndexOrSlice
();
}
case
Scanner
.
LPAREN
:
P
.
ParseInvocation
();
default
:
P
.
Ecart
();
return
;
}
}
P
.
Next
();
}
else
{
P
.
ParseVarSpec
();
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseParameterSection
()
{
func
(
P
*
Parser
)
ParsePrimaryExprList
()
{
P
.
Trace
(
"ParameterSection"
);
P
.
Trace
(
"PrimaryExprList"
);
P
.
ParseIdentList
();
P
.
ParsePrimaryExpr
();
P
.
ParseType
();
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseParameterList
()
{
P
.
Trace
(
"ParameterList"
);
P
.
ParseParameterSection
();
for
P
.
tok
==
Scanner
.
COMMA
{
for
P
.
tok
==
Scanner
.
COMMA
{
P
.
Next
();
P
.
Next
();
P
.
ParseP
arameterSection
();
P
.
ParseP
rimaryExpr
();
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseParameters
()
{
func
(
P
*
Parser
)
ParseUnaryExpr
()
{
P
.
Trace
(
"Parameters"
);
P
.
Trace
(
"UnaryExpr"
);
P
.
Expect
(
Scanner
.
LPAREN
);
switch
P
.
tok
{
if
P
.
tok
!=
Scanner
.
RPAREN
{
case
Scanner
.
ADD
:
fallthrough
;
P
.
ParseParameterList
();
case
Scanner
.
SUB
:
fallthrough
;
case
Scanner
.
NOT
:
fallthrough
;
case
Scanner
.
XOR
:
fallthrough
;
case
Scanner
.
LSS
:
fallthrough
;
case
Scanner
.
GTR
:
fallthrough
;
case
Scanner
.
MUL
:
fallthrough
;
case
Scanner
.
AND
:
P
.
Next
();
P
.
ParseUnaryExpr
();
P
.
Ecart
();
return
;
}
}
P
.
Expect
(
Scanner
.
RPAREN
);
P
.
ParsePrimaryExpr
(
);
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
TryResult
()
bool
{
func
Precedence
(
tok
int
)
int
{
P
.
Trace
(
"Result (try)"
);
// TODO should use a map or array here for lookup
res
:=
false
;
switch
tok
{
if
P
.
tok
==
Scanner
.
LPAREN
{
case
Scanner
.
LOR
:
// TODO: here we allow empty returns - should proably fix this
return
1
;
P
.
ParseParameters
();
case
Scanner
.
LAND
:
res
=
true
;
return
2
;
}
else
{
case
Scanner
.
EQL
,
Scanner
.
NEQ
,
Scanner
.
LSS
,
Scanner
.
LEQ
,
Scanner
.
GTR
,
Scanner
.
GEQ
:
res
=
P
.
TryType
();
return
3
;
case
Scanner
.
ADD
,
Scanner
.
SUB
,
Scanner
.
OR
,
Scanner
.
XOR
:
return
4
;
case
Scanner
.
MUL
,
Scanner
.
QUO
,
Scanner
.
REM
,
Scanner
.
SHL
,
Scanner
.
SHR
,
Scanner
.
AND
:
return
5
;
}
}
P
.
Ecart
();
return
0
;
return
res
;
}
}
// Anonymous signatures
func
(
P
*
Parser
)
ParseBinaryExpr
(
prec1
int
)
{
//
P
.
Trace
(
"BinaryExpr"
);
// (params)
P
.
ParseUnaryExpr
();
// (params) type
for
prec
:=
Precedence
(
P
.
tok
);
prec
>=
prec1
;
prec
--
{
// (params) (results)
for
Precedence
(
P
.
tok
)
==
prec
{
// (recv) . (params)
// (recv) . (params) type
// (recv) . (params) (results)
func
(
P
*
Parser
)
ParseAnonymousSignature
()
{
P
.
Trace
(
"AnonymousSignature"
);
P
.
OpenScope
();
P
.
ParseParameters
();
if
P
.
tok
==
Scanner
.
PERIOD
{
P
.
Next
();
P
.
Next
();
P
.
ParseParameters
(
);
P
.
ParseBinaryExpr
(
prec
+
1
);
}
}
P
.
TryResult
();
P
.
CloseScope
();
P
.
Ecart
();
}
// Named signatures
//
// name (params)
// name (params) type
// name (params) (results)
// (recv) name (params)
// (recv) name (params) type
// (recv) name (params) (results)
func
(
P
*
Parser
)
ParseNamedSignature
()
{
P
.
Trace
(
"NamedSignature"
);
P
.
OpenScope
();
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
ParseParameters
();
}
}
P
.
ParseIdent
();
// function name
P
.
ParseParameters
();
P
.
TryResult
();
P
.
CloseScope
();
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseDeclaration
();
func
(
P
*
Parser
)
ParseExpression
()
{
func
(
P
*
Parser
)
TryStatement
()
bool
;
P
.
Trace
(
"Expression"
);
func
(
P
*
Parser
)
ParseStatementList
();
indent
:=
P
.
indent
;
func
(
P
*
Parser
)
ParseBlock
();
P
.
ParseBinaryExpr
(
1
);
func
(
P
*
Parser
)
ParsePrimaryExpr
();
if
indent
!=
P
.
indent
{
panic
"imbalanced tracing code"
;
func
(
P
*
Parser
)
ParsePrimaryExprList
()
{
P
.
Trace
(
"PrimaryExprList"
);
P
.
ParsePrimaryExpr
();
for
P
.
tok
==
Scanner
.
COMMA
{
P
.
Next
();
P
.
ParsePrimaryExpr
();
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
// ----------------------------------------------------------------------------
// Statements
func
(
P
*
Parser
)
ParseBuiltinStat
()
{
func
(
P
*
Parser
)
ParseBuiltinStat
()
{
P
.
Trace
(
"BuiltinStat"
);
P
.
Trace
(
"BuiltinStat"
);
P
.
Expect
(
Scanner
.
IDENT
);
P
.
Expect
(
Scanner
.
IDENT
);
...
@@ -631,15 +712,6 @@ func (P *Parser) ParseControlFlowStat(tok int) {
...
@@ -631,15 +712,6 @@ func (P *Parser) ParseControlFlowStat(tok int) {
}
}
func
(
P
*
Parser
)
ParseStatement
()
{
P
.
Trace
(
"Statement"
);
if
!
P
.
TryStatement
()
{
P
.
Error
(
P
.
beg
,
"statement expected"
);
}
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseIfStat
()
{
func
(
P
*
Parser
)
ParseIfStat
()
{
P
.
Trace
(
"IfStat"
);
P
.
Trace
(
"IfStat"
);
P
.
Expect
(
Scanner
.
IF
);
P
.
Expect
(
Scanner
.
IF
);
...
@@ -870,258 +942,194 @@ func (P *Parser) TryStatement() bool {
...
@@ -870,258 +942,194 @@ func (P *Parser) TryStatement() bool {
}
}
func
(
P
*
Parser
)
ParseStatementList
()
{
// ----------------------------------------------------------------------------
P
.
Trace
(
"StatementList"
);
// Declarations
for
P
.
TryStatement
()
{
P
.
Optional
(
Scanner
.
SEMICOLON
);
}
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseBlock
()
{
P
.
Trace
(
"Block"
);
P
.
Expect
(
Scanner
.
LBRACE
);
P
.
OpenScope
();
if
P
.
tok
!=
Scanner
.
RBRACE
&&
P
.
tok
!=
Scanner
.
SEMICOLON
{
P
.
ParseStatementList
();
}
P
.
Optional
(
Scanner
.
SEMICOLON
);
P
.
CloseScope
();
P
.
Expect
(
Scanner
.
RBRACE
);
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseFuncDecl
()
{
func
(
P
*
Parser
)
ParseImportSpec
()
{
P
.
Trace
(
"FuncDecl"
);
P
.
Trace
(
"ImportSpec"
);
P
.
Expect
(
Scanner
.
FUNC
);
if
P
.
tok
==
Scanner
.
PERIOD
{
P
.
ParseNamedSignature
();
P
.
Next
();
if
P
.
tok
==
Scanner
.
SEMICOLON
{
}
else
if
P
.
tok
==
Scanner
.
IDENT
{
// forward declaration
P
.
Next
();
P
.
Next
();
}
else
{
P
.
ParseBlock
();
}
}
P
.
Expect
(
Scanner
.
STRING
);
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
Parse
Ex
portDecl
()
{
func
(
P
*
Parser
)
Parse
Im
portDecl
()
{
P
.
Trace
(
"
Ex
portDecl"
);
P
.
Trace
(
"
Im
portDecl"
);
P
.
Expect
(
Scanner
.
EX
PORT
);
P
.
Expect
(
Scanner
.
IM
PORT
);
if
P
.
tok
==
Scanner
.
LPAREN
{
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
Next
();
P
.
Next
();
for
P
.
tok
!=
Scanner
.
RPAREN
{
for
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
ParseI
dent
();
P
.
ParseI
mportSpec
();
P
.
Optional
(
Scanner
.
COMMA
);
// TODO this seems wrong
P
.
Optional
(
Scanner
.
SEMICOLON
);
// TODO this seems wrong
}
}
P
.
Next
();
P
.
Next
();
}
else
{
}
else
{
P
.
ParseIdent
();
P
.
ParseImportSpec
();
for
P
.
tok
==
Scanner
.
COMMA
{
P
.
Next
();
P
.
ParseIdent
();
}
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseDeclaration
()
{
func
(
P
*
Parser
)
ParseConstSpec
()
{
P
.
Trace
(
"Declaration"
);
P
.
Trace
(
"ConstSpec"
);
indent
:=
P
.
indent
;
P
.
ParseIdent
();
switch
P
.
tok
{
P
.
TryType
();
case
Scanner
.
CONST
:
if
P
.
tok
==
Scanner
.
ASSIGN
{
P
.
ParseConstDecl
();
P
.
Next
();
case
Scanner
.
TYPE
:
P
.
ParseExpression
();
P
.
ParseTypeDecl
();
case
Scanner
.
VAR
:
P
.
ParseVarDecl
();
case
Scanner
.
FUNC
:
P
.
ParseFuncDecl
();
case
Scanner
.
EXPORT
:
P
.
ParseExportDecl
();
default
:
P
.
Error
(
P
.
beg
,
"declaration expected"
);
}
if
indent
!=
P
.
indent
{
panic
"imbalanced tracing code"
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseNew
()
{
func
(
P
*
Parser
)
ParseConstDecl
()
{
P
.
Trace
(
"New"
);
P
.
Trace
(
"ConstDecl"
);
P
.
Expect
(
Scanner
.
NEW
);
P
.
Expect
(
Scanner
.
CONST
);
P
.
Expect
(
Scanner
.
LPAREN
);
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
ParseType
();
if
P
.
tok
==
Scanner
.
COMMA
{
P
.
Next
();
P
.
Next
();
P
.
ParseExpressionList
()
for
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
ParseConstSpec
();
if
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
Expect
(
Scanner
.
SEMICOLON
);
}
}
P
.
Next
();
}
else
{
P
.
ParseConstSpec
();
}
}
P
.
Expect
(
Scanner
.
RPAREN
);
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
Parse
FunctionLit
()
{
func
(
P
*
Parser
)
Parse
TypeSpec
()
{
P
.
Trace
(
"
FunctionLit
"
);
P
.
Trace
(
"
TypeSpec
"
);
P
.
Parse
FunctionType
();
P
.
Parse
Ident
();
P
.
ParseBlock
();
P
.
TryType
();
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseOperand
()
{
func
(
P
*
Parser
)
ParseTypeDecl
()
{
P
.
Trace
(
"Operand"
);
P
.
Trace
(
"TypeDecl"
);
switch
P
.
tok
{
P
.
Expect
(
Scanner
.
TYPE
);
case
Scanner
.
IDENT
:
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
ParseQualifiedIdent
();
case
Scanner
.
LPAREN
:
P
.
Next
();
P
.
Next
();
P
.
ParseExpression
();
for
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
Expect
(
Scanner
.
RPAREN
);
P
.
ParseTypeSpec
();
case
Scanner
.
STRING
:
fallthrough
;
if
P
.
tok
!=
Scanner
.
RPAREN
{
case
Scanner
.
NUMBER
:
fallthrough
;
P
.
Expect
(
Scanner
.
SEMICOLON
);
case
Scanner
.
NIL
:
fallthrough
;
}
case
Scanner
.
IOTA
:
fallthrough
;
}
case
Scanner
.
TRUE
:
fallthrough
;
case
Scanner
.
FALSE
:
P
.
Next
();
P
.
Next
();
case
Scanner
.
FUNC
:
}
else
{
P
.
ParseFunctionLit
();
P
.
ParseTypeSpec
();
case
Scanner
.
NEW
:
P
.
ParseNew
();
default
:
P
.
Error
(
P
.
beg
,
"operand expected"
);
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseSelectorOrTypeAssertion
()
{
func
(
P
*
Parser
)
ParseVarSpec
()
{
P
.
Trace
(
"SelectorOrTypeAssertion"
);
P
.
Trace
(
"VarSpec"
);
P
.
Expect
(
Scanner
.
PERIOD
);
P
.
ParseIdentList
();
if
P
.
tok
==
Scanner
.
IDENT
{
if
P
.
tok
==
Scanner
.
ASSIGN
{
P
.
ParseIdent
();
P
.
Next
();
P
.
ParseExpressionList
();
}
else
{
}
else
{
P
.
Expect
(
Scanner
.
LPAREN
);
P
.
ParseType
();
P
.
ParseType
();
P
.
Expect
(
Scanner
.
RPAREN
);
if
P
.
tok
==
Scanner
.
ASSIGN
{
}
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParseIndexOrSlice
()
{
P
.
Trace
(
"IndexOrSlice"
);
P
.
Expect
(
Scanner
.
LBRACK
);
P
.
ParseExpression
();
if
P
.
tok
==
Scanner
.
COLON
{
P
.
Next
();
P
.
Next
();
P
.
ParseExpression
();
P
.
ParseExpressionList
();
}
}
}
P
.
Expect
(
Scanner
.
RBRACK
);
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseInvocation
()
{
func
(
P
*
Parser
)
ParseVarDecl
()
{
P
.
Trace
(
"Invocation"
);
P
.
Trace
(
"VarDecl"
);
P
.
Expect
(
Scanner
.
LPAREN
);
P
.
Expect
(
Scanner
.
VAR
);
if
P
.
tok
==
Scanner
.
LPAREN
{
P
.
Next
();
for
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
ParseVarSpec
();
if
P
.
tok
!=
Scanner
.
RPAREN
{
if
P
.
tok
!=
Scanner
.
RPAREN
{
P
.
ParseExpressionList
(
);
P
.
Expect
(
Scanner
.
SEMICOLON
);
}
}
P
.
Expect
(
Scanner
.
RPAREN
);
P
.
Ecart
();
}
func
(
P
*
Parser
)
ParsePrimaryExpr
()
{
P
.
Trace
(
"PrimaryExpr"
);
P
.
ParseOperand
();
for
{
switch
P
.
tok
{
case
Scanner
.
PERIOD
:
P
.
ParseSelectorOrTypeAssertion
();
case
Scanner
.
LBRACK
:
P
.
ParseIndexOrSlice
();
case
Scanner
.
LPAREN
:
P
.
ParseInvocation
();
default
:
P
.
Ecart
();
return
;
}
}
P
.
Next
();
}
else
{
P
.
ParseVarSpec
();
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
ParseUnaryExpr
()
{
func
(
P
*
Parser
)
ParseFuncDecl
()
{
P
.
Trace
(
"UnaryExpr"
);
P
.
Trace
(
"FuncDecl"
);
switch
P
.
tok
{
P
.
Expect
(
Scanner
.
FUNC
);
case
Scanner
.
ADD
:
fallthrough
;
P
.
ParseNamedSignature
();
case
Scanner
.
SUB
:
fallthrough
;
if
P
.
tok
==
Scanner
.
SEMICOLON
{
case
Scanner
.
NOT
:
fallthrough
;
// forward declaration
case
Scanner
.
XOR
:
fallthrough
;
case
Scanner
.
LSS
:
fallthrough
;
case
Scanner
.
GTR
:
fallthrough
;
case
Scanner
.
MUL
:
fallthrough
;
case
Scanner
.
AND
:
P
.
Next
();
P
.
Next
();
P
.
ParseUnaryExpr
();
}
else
{
P
.
Ecart
();
P
.
ParseBlock
();
return
;
}
}
P
.
ParsePrimaryExpr
();
P
.
Ecart
();
P
.
Ecart
();
}
}
func
Precedence
(
tok
int
)
int
{
func
(
P
*
Parser
)
ParseExportDecl
()
{
// TODO should use a map or array here for lookup
P
.
Trace
(
"ExportDecl"
);
switch
tok
{
P
.
Expect
(
Scanner
.
EXPORT
);
case
Scanner
.
LOR
:
if
P
.
tok
==
Scanner
.
LPAREN
{
return
1
;
P
.
Next
();
case
Scanner
.
LAND
:
for
P
.
tok
!=
Scanner
.
RPAREN
{
return
2
;
P
.
ParseIdent
();
case
Scanner
.
EQL
,
Scanner
.
NEQ
,
Scanner
.
LSS
,
Scanner
.
LEQ
,
Scanner
.
GTR
,
Scanner
.
GEQ
:
P
.
Optional
(
Scanner
.
COMMA
);
// TODO this seems wrong
return
3
;
case
Scanner
.
ADD
,
Scanner
.
SUB
,
Scanner
.
OR
,
Scanner
.
XOR
:
return
4
;
case
Scanner
.
MUL
,
Scanner
.
QUO
,
Scanner
.
REM
,
Scanner
.
SHL
,
Scanner
.
SHR
,
Scanner
.
AND
:
return
5
;
}
}
return
0
;
}
func
(
P
*
Parser
)
ParseBinaryExpr
(
prec1
int
)
{
P
.
Trace
(
"BinaryExpr"
);
P
.
ParseUnaryExpr
();
for
prec
:=
Precedence
(
P
.
tok
);
prec
>=
prec1
;
prec
--
{
for
Precedence
(
P
.
tok
)
==
prec
{
P
.
Next
();
P
.
Next
();
P
.
ParseBinaryExpr
(
prec
+
1
);
}
else
{
P
.
ParseIdent
();
for
P
.
tok
==
Scanner
.
COMMA
{
P
.
Next
();
P
.
ParseIdent
();
}
}
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
func
(
P
*
Parser
)
Parse
Express
ion
()
{
func
(
P
*
Parser
)
Parse
Declarat
ion
()
{
P
.
Trace
(
"
Express
ion"
);
P
.
Trace
(
"
Declarat
ion"
);
indent
:=
P
.
indent
;
indent
:=
P
.
indent
;
P
.
ParseBinaryExpr
(
1
);
switch
P
.
tok
{
case
Scanner
.
CONST
:
P
.
ParseConstDecl
();
case
Scanner
.
TYPE
:
P
.
ParseTypeDecl
();
case
Scanner
.
VAR
:
P
.
ParseVarDecl
();
case
Scanner
.
FUNC
:
P
.
ParseFuncDecl
();
case
Scanner
.
EXPORT
:
P
.
ParseExportDecl
();
default
:
P
.
Error
(
P
.
beg
,
"declaration expected"
);
}
if
indent
!=
P
.
indent
{
if
indent
!=
P
.
indent
{
panic
"imbalanced tracing code"
;
panic
"imbalanced tracing code"
}
}
P
.
Ecart
();
P
.
Ecart
();
}
}
// ----------------------------------------------------------------------------
// Program
func
(
P
*
Parser
)
ParseProgram
()
{
func
(
P
*
Parser
)
ParseProgram
()
{
P
.
Trace
(
"Program"
);
P
.
Trace
(
"Program"
);
P
.
OpenScope
();
P
.
OpenScope
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment