Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
835cd469
Commit
835cd469
authored
Jul 08, 2008
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- first cut a Go parser in Go
SVN=126242
parent
999b12c7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
77 additions
and
13 deletions
+77
-13
go_lang.txt
doc/go_lang.txt
+6
-5
parser.go
usr/gri/src/parser.go
+0
-0
scanner.go
usr/gri/src/scanner.go
+29
-6
test_parser.go
usr/gri/src/test_parser.go
+40
-0
test_scanner.go
usr/gri/src/test_scanner.go
+2
-2
No files found.
doc/go_lang.txt
View file @
835cd469
...
...
@@ -4,7 +4,7 @@ The Go Programming Language (DRAFT)
Robert Griesemer, Rob Pike, Ken Thompson
----
(July
3
, 2008)
(July
7
, 2008)
This document is a semi-formal specification/proposal for a new
systems programming language. The document is under active
...
...
@@ -314,10 +314,11 @@ Reserved words
break fallthrough import return
case false interface select
const for map struct
continue func new switch
default go nil true
else goto package type
export if range var
chan func new switch
continue go nil true
default goto package type
else if range var
export
TODO: "len" is currently also a reserved word - it shouldn't be.
...
...
usr/gri/src/parser.go
0 → 100644
View file @
835cd469
This diff is collapsed.
Click to expand it.
usr/gri/src/scanner.go
View file @
835cd469
...
...
@@ -4,7 +4,24 @@
package
Scanner
export
EOF
;
export
ILLEGAL
,
EOF
,
IDENT
,
STRING
,
NUMBER
,
COMMA
,
COLON
,
SEMICOLON
,
PERIOD
,
LPAREN
,
RPAREN
,
LBRACK
,
RBRACK
,
LBRACE
,
RBRACE
,
ASSIGN
,
DEFINE
,
INC
,
DEC
,
NOT
,
AND
,
OR
,
XOR
,
ADD
,
SUB
,
MUL
,
QUO
,
REM
,
EQL
,
NEQ
,
LSS
,
LEQ
,
GTR
,
GEQ
,
SHL
,
SHR
,
ADD_ASSIGN
,
SUB_ASSIGN
,
MUL_ASSIGN
,
QUO_ASSIGN
,
REM_ASSIGN
,
AND_ASSIGN
,
OR_ASSIGN
,
XOR_ASSIGN
,
SHL_ASSIGN
,
SHR_ASSIGN
,
CAND
,
COR
,
BREAK
,
CASE
,
CHAN
,
CONST
,
CONTINUE
,
DEFAULT
,
ELSE
,
EXPORT
,
FALLTHROUGH
,
FALSE
,
FOR
,
FUNC
,
GO
,
GOTO
,
IF
,
IMPORT
,
INTERFACE
,
MAP
,
NEW
,
NIL
,
PACKAGE
,
RANGE
,
RETURN
,
SELECT
,
STRUCT
,
SWITCH
,
TRUE
,
TYPE
,
VAR
const
(
ILLEGAL
=
iota
;
EOF
;
...
...
@@ -71,6 +88,7 @@ const (
KEYWORDS_BEG
;
BREAK
;
CASE
;
CHAN
;
CONST
;
CONTINUE
;
DEFAULT
;
...
...
@@ -170,6 +188,7 @@ func TokenName(tok int) string {
case
BREAK
:
return
"break"
;
case
CASE
:
return
"case"
;
case
CHAN
:
return
"chan"
;
case
CONST
:
return
"const"
;
case
CONTINUE
:
return
"continue"
;
case
DEFAULT
:
return
"default"
;
...
...
@@ -234,6 +253,7 @@ type Scanner struct {
}
/*
export Token
type Token struct {
val int;
...
...
@@ -245,6 +265,7 @@ type Token struct {
func (T *Token) Print () {
print TokenName(T.val), " [", T.beg, ", ", T.end, "[ ", T.txt, "\n";
}
*/
// Read the next Unicode char into S.ch.
...
...
@@ -601,12 +622,12 @@ func (S *Scanner) Select4 (tok0, tok1, ch2, tok2, tok3 int) int {
}
func
(
S
*
Scanner
)
Scan
(
t
*
Token
)
(
tok
,
beg
,
end
int
)
{
func
(
S
*
Scanner
)
Scan
()
(
tok
,
beg
,
end
int
)
{
S
.
SkipWhitespace
();
var
tok
int
=
ILLEGAL
;
var
beg
int
=
S
.
pos
-
1
;
var
end
int
=
beg
;
tok
=
ILLEGAL
;
beg
=
S
.
pos
-
1
;
end
=
beg
;
ch
:=
S
.
ch
;
switch
{
...
...
@@ -641,7 +662,7 @@ func (S *Scanner) Scan (t *Token) (tok, beg, end int) {
if
S
.
ch
==
'/'
||
S
.
ch
==
'*'
{
S
.
SkipComment
();
// cannot simply return because of 6g bug
tok
,
beg
,
end
=
S
.
Scan
(
t
);
tok
,
beg
,
end
=
S
.
Scan
();
return
tok
,
beg
,
end
;
}
tok
=
S
.
Select2
(
QUO
,
QUO_ASSIGN
);
...
...
@@ -659,10 +680,12 @@ func (S *Scanner) Scan (t *Token) (tok, beg, end int) {
end
=
S
.
pos
-
1
;
/*
t.val = tok;
t.beg = beg;
t.end = end;
t.txt = S.src[beg : end];
*/
return
tok
,
beg
,
end
;
}
usr/gri/src/test_parser.go
0 → 100644
View file @
835cd469
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
main
import
Scanner
"scanner"
import
Parser
"parser"
func
Parse
(
src
string
,
verbose
bool
)
{
S
:=
new
(
Scanner
.
Scanner
);
S
.
Open
(
src
);
P
:=
new
(
Parser
.
Parser
);
P
.
Open
(
S
,
verbose
);
P
.
ParseProgram
();
}
func
main
()
{
verbose
:=
false
;
for
i
:=
1
;
i
<
sys
.
argc
();
i
++
{
if
sys
.
argv
(
i
)
==
"-v"
{
verbose
=
true
;
continue
;
}
var
src
string
;
var
ok
bool
;
src
,
ok
=
sys
.
readfile
(
sys
.
argv
(
i
));
if
ok
{
print
"parsing "
+
sys
.
argv
(
i
)
+
"
\n
"
;
Parse
(
src
,
verbose
);
}
else
{
print
"error: cannot read "
+
sys
.
argv
(
i
)
+
"
\n
"
;
}
}
}
usr/gri/src/test_scanner.go
View file @
835cd469
...
...
@@ -11,9 +11,9 @@ func Scan(src string) {
S
:=
new
(
Scanner
.
Scanner
);
S
.
Open
(
src
);
for
{
var
t
Scanner
.
Token
;
//
var t Scanner.Token;
var
tok
,
beg
,
end
int
;
tok
,
beg
,
end
=
S
.
Scan
(
&
t
);
tok
,
beg
,
end
=
S
.
Scan
(
/*&t*/
);
//t.Print(); // TODO this doesn't compile?
print
Scanner
.
TokenName
(
tok
),
"
\t
"
,
src
[
beg
:
end
],
"
\n
"
;
if
tok
==
Scanner
.
EOF
{
...
...
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