Commit 835cd469 authored by Robert Griesemer's avatar Robert Griesemer

- first cut a Go parser in Go

SVN=126242
parent 999b12c7
...@@ -4,7 +4,7 @@ The Go Programming Language (DRAFT) ...@@ -4,7 +4,7 @@ The Go Programming Language (DRAFT)
Robert Griesemer, Rob Pike, Ken Thompson Robert Griesemer, Rob Pike, Ken Thompson
---- ----
(July 3, 2008) (July 7, 2008)
This document is a semi-formal specification/proposal for a new This document is a semi-formal specification/proposal for a new
systems programming language. The document is under active systems programming language. The document is under active
...@@ -314,10 +314,11 @@ Reserved words ...@@ -314,10 +314,11 @@ Reserved words
break fallthrough import return break fallthrough import return
case false interface select case false interface select
const for map struct const for map struct
continue func new switch chan func new switch
default go nil true continue go nil true
else goto package type default goto package type
export if range var else if range var
export
TODO: "len" is currently also a reserved word - it shouldn't be. TODO: "len" is currently also a reserved word - it shouldn't be.
......
This diff is collapsed.
...@@ -4,7 +4,24 @@ ...@@ -4,7 +4,24 @@
package Scanner 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 ( const (
ILLEGAL = iota; ILLEGAL = iota;
EOF; EOF;
...@@ -71,6 +88,7 @@ const ( ...@@ -71,6 +88,7 @@ const (
KEYWORDS_BEG; KEYWORDS_BEG;
BREAK; BREAK;
CASE; CASE;
CHAN;
CONST; CONST;
CONTINUE; CONTINUE;
DEFAULT; DEFAULT;
...@@ -170,6 +188,7 @@ func TokenName(tok int) string { ...@@ -170,6 +188,7 @@ func TokenName(tok int) string {
case BREAK: return "break"; case BREAK: return "break";
case CASE: return "case"; case CASE: return "case";
case CHAN: return "chan";
case CONST: return "const"; case CONST: return "const";
case CONTINUE: return "continue"; case CONTINUE: return "continue";
case DEFAULT: return "default"; case DEFAULT: return "default";
...@@ -234,6 +253,7 @@ type Scanner struct { ...@@ -234,6 +253,7 @@ type Scanner struct {
} }
/*
export Token export Token
type Token struct { type Token struct {
val int; val int;
...@@ -245,6 +265,7 @@ type Token struct { ...@@ -245,6 +265,7 @@ type Token struct {
func (T *Token) Print () { func (T *Token) Print () {
print TokenName(T.val), " [", T.beg, ", ", T.end, "[ ", T.txt, "\n"; print TokenName(T.val), " [", T.beg, ", ", T.end, "[ ", T.txt, "\n";
} }
*/
// Read the next Unicode char into S.ch. // Read the next Unicode char into S.ch.
...@@ -601,12 +622,12 @@ func (S *Scanner) Select4 (tok0, tok1, ch2, tok2, tok3 int) int { ...@@ -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(); S.SkipWhitespace();
var tok int = ILLEGAL; tok = ILLEGAL;
var beg int = S.pos - 1; beg = S.pos - 1;
var end int = beg; end = beg;
ch := S.ch; ch := S.ch;
switch { switch {
...@@ -641,7 +662,7 @@ func (S *Scanner) Scan (t *Token) (tok, beg, end int) { ...@@ -641,7 +662,7 @@ func (S *Scanner) Scan (t *Token) (tok, beg, end int) {
if S.ch == '/' || S.ch == '*' { if S.ch == '/' || S.ch == '*' {
S.SkipComment(); S.SkipComment();
// cannot simply return because of 6g bug // cannot simply return because of 6g bug
tok, beg, end = S.Scan(t); tok, beg, end = S.Scan();
return tok, beg, end; return tok, beg, end;
} }
tok = S.Select2(QUO, QUO_ASSIGN); tok = S.Select2(QUO, QUO_ASSIGN);
...@@ -659,10 +680,12 @@ func (S *Scanner) Scan (t *Token) (tok, beg, end int) { ...@@ -659,10 +680,12 @@ func (S *Scanner) Scan (t *Token) (tok, beg, end int) {
end = S.pos - 1; end = S.pos - 1;
/*
t.val = tok; t.val = tok;
t.beg = beg; t.beg = beg;
t.end = end; t.end = end;
t.txt = S.src[beg : end]; t.txt = S.src[beg : end];
*/
return tok, beg, end; return tok, beg, end;
} }
// 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";
}
}
}
...@@ -11,9 +11,9 @@ func Scan(src string) { ...@@ -11,9 +11,9 @@ func Scan(src string) {
S := new(Scanner.Scanner); S := new(Scanner.Scanner);
S.Open(src); S.Open(src);
for { for {
var t Scanner.Token; //var t Scanner.Token;
var tok, beg, end int; var tok, beg, end int;
tok, beg, end = S.Scan(&t); tok, beg, end = S.Scan(/*&t*/);
//t.Print(); // TODO this doesn't compile? //t.Print(); // TODO this doesn't compile?
print Scanner.TokenName(tok), "\t ", src[beg : end], "\n"; print Scanner.TokenName(tok), "\t ", src[beg : end], "\n";
if tok == Scanner.EOF { if tok == Scanner.EOF {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment