Commit ed9743dc authored by Robert Griesemer's avatar Robert Griesemer

- more Go scanner work

SVN=125831
parent 85728a2d
...@@ -32,10 +32,9 @@ const ( ...@@ -32,10 +32,9 @@ const (
DEC = iota; DEC = iota;
NOT = iota; NOT = iota;
OR = iota;
BOR = iota;
AND = iota; AND = iota;
BAND = iota; OR = iota;
XOR = iota;
ADD = iota; ADD = iota;
SUB = iota; SUB = iota;
...@@ -50,6 +49,25 @@ const ( ...@@ -50,6 +49,25 @@ const (
GTR = iota; GTR = iota;
GEQ = iota; GEQ = iota;
SHL = iota;
SHR = iota;
ADD_ASSIGN = iota;
SUB_ASSIGN = iota;
MUL_ASSIGN = iota;
QUO_ASSIGN = iota;
REM_ASSIGN = iota;
AND_ASSIGN = iota;
OR_ASSIGN = iota;
XOR_ASSIGN = iota;
SHL_ASSIGN = iota;
SHR_ASSIGN = iota;
CAND = iota;
COR = iota;
// keywords // keywords
BREAK = iota; BREAK = iota;
CASE = iota; CASE = iota;
...@@ -115,10 +133,9 @@ func TokenName(tok int) string { ...@@ -115,10 +133,9 @@ func TokenName(tok int) string {
case DEC: return "DEC"; case DEC: return "DEC";
case NOT: return "NOT"; case NOT: return "NOT";
case OR: return "OR";
case BOR: return "BOR";
case AND: return "AND"; case AND: return "AND";
case BAND: return "BAND"; case OR: return "OR";
case XOR: return "XOR";
case ADD: return "ADD"; case ADD: return "ADD";
case SUB: return "SUB"; case SUB: return "SUB";
...@@ -134,6 +151,25 @@ func TokenName(tok int) string { ...@@ -134,6 +151,25 @@ func TokenName(tok int) string {
case GTR: return "GTR"; case GTR: return "GTR";
case GEQ: return "GEQ"; case GEQ: return "GEQ";
case SHL: return SHL;
case SHR: return SHR;
case ADD_ASSIGN: return "ADD_ASSIGN";
case SUB_ASSIGN: return "SUB_ASSIGN";
case MUL_ASSIGN: return "MUL_ASSIGN";
case QUO_ASSIGN: return "QUO_ASSIGN";
case REM_ASSIGN: return "REM_ASSIGN";
case AND_ASSIGN: return "AND_ASSIGN";
case OR_ASSIGN: return "OR_ASSIGN";
case XOR_ASSIGN: return "XOR_ASSIGN";
case SHL_ASSIGN: return "SHL_ASSIGN";
case SHR_ASSIGN: return "SHR_ASSIGN";
case CAND: return "CAND";
case COR: return "COR";
case BREAK: return "BREAK"; case BREAK: return "BREAK";
case CASE: return "CASE"; case CASE: return "CASE";
case CONST: return "CONST"; case CONST: return "CONST";
...@@ -305,15 +341,30 @@ func (S *Scanner) ScanIdentifier () int { ...@@ -305,15 +341,30 @@ func (S *Scanner) ScanIdentifier () int {
} }
func (S *Scanner) ScanNumber () { func (S *Scanner) ScanMantissa () {
// TODO complete this routine
for is_dec_digit(S.ch) { for is_dec_digit(S.ch) {
S.Next(); S.Next();
} }
} }
func (S *Scanner) ScanNumber () int {
// TODO complete this routine
if S.ch == '.' {
S.Next();
}
S.ScanMantissa();
if S.ch == 'e' || S.ch == 'E' {
S.Next();
if S.ch == '-' || S.ch == '+' {
S.Next();
}
S.ScanMantissa();
}
return NUMBER;
}
func (S *Scanner) ScanOctDigits(n int) { func (S *Scanner) ScanOctDigits(n int) {
for ; n > 0; n-- { for ; n > 0; n-- {
if !is_oct_digit(S.ch) { if !is_oct_digit(S.ch) {
...@@ -371,9 +422,7 @@ func (S *Scanner) ScanEscape () { ...@@ -371,9 +422,7 @@ func (S *Scanner) ScanEscape () {
} }
func (S *Scanner) ScanChar () { func (S *Scanner) ScanChar () int {
S.Next(); // consume '\'
if (S.ch == '\\') { if (S.ch == '\\') {
S.Next(); S.Next();
S.ScanEscape(); S.ScanEscape();
...@@ -386,201 +435,124 @@ func (S *Scanner) ScanChar () { ...@@ -386,201 +435,124 @@ func (S *Scanner) ScanChar () {
} else { } else {
panic "char not terminated"; panic "char not terminated";
} }
return NUMBER;
} }
func (S *Scanner) ScanString () { func (S *Scanner) ScanString () int {
for S.Next(); S.ch != '"'; S.Next() { for ; S.ch != '"'; S.Next() {
if S.ch == '\n' || S.ch < 0 { if S.ch == '\n' || S.ch < 0 {
panic "string not terminated"; panic "string not terminated";
} }
} }
S.Next(); S.Next();
return STRING;
} }
func (S *Scanner) ScanRawString () { func (S *Scanner) ScanRawString () int {
for S.Next(); S.ch != '`'; S.Next() { for ; S.ch != '`'; S.Next() {
if S.ch == '\n' || S.ch < 0 { if S.ch == '\n' || S.ch < 0 {
panic "string not terminated"; panic "string not terminated";
} }
} }
S.Next(); S.Next();
return STRING;
} }
func (S *Scanner) Scan () (tok, beg, end int) { func (S *Scanner) Select2 (tok0, tok1 int) int {
S.SkipWhitespace(); if S.ch == '=' {
var tok int = ILLEGAL;
var beg int = S.pos - 1;
var end int = beg;
if is_letter(S.ch) {
tok = S.ScanIdentifier();
} else if is_dec_digit(S.ch) {
S.ScanNumber();
tok = NUMBER;
} else {
switch S.ch {
case -1:
tok = EOF;
case '/':
S.Next(); S.Next();
if S.ch == '/' || S.ch == '*' { return tok1;
S.SkipComment();
tok, beg, end = S.Scan();
return tok, beg, end;
} else {
tok = QUO;
} }
return tok0;
}
case '"':
S.ScanString();
tok = STRING;
case '\'':
S.ScanChar();
tok = NUMBER;
case '`':
S.ScanRawString();
tok = STRING;
case ':': func (S *Scanner) Select3 (tok0, tok1, ch2, tok2 int) int {
if S.ch == '=' {
S.Next(); S.Next();
if (S.ch == '=') { return tok1;
S.Next();
tok = DEFINE;
} else {
tok = COLON;
} }
if S.ch == ch2 {
case '.':
S.Next();
tok = PERIOD;
case ',':
S.Next();
tok = COMMA;
case '+':
S.Next();
if (S.ch == '+') {
S.Next(); S.Next();
tok = INC; return tok2;
} else {
tok = ADD;
}
case '-':
S.Next();
if (S.ch == '-') {
S.Next();
tok = DEC;
} else {
tok = SUB;
} }
return tok0;
}
case '*':
S.Next();
tok = MUL;
case '/':
S.Next();
tok = QUO;
case '%': func (S *Scanner) Select4 (tok0, tok1, ch2, tok2, tok3 int) int {
if S.ch == '=' {
S.Next(); S.Next();
tok = REM; return tok1;
case '<':
S.Next();
if (S.ch == '=') {
S.Next();
tok = LEQ;
} else {
tok = LSS;
} }
if S.ch == ch2 {
case '>':
S.Next(); S.Next();
if (S.ch == '=') { if S.ch == '=' {
S.Next(); S.Next();
tok = GEQ; return tok3;
} else {
tok = GTR;
}
case '=':
S.Next();
if (S.ch == '=') {
S.Next();
tok = EQL;
} else {
tok = ASSIGN;
} }
return tok2;
case '!':
S.Next();
if (S.ch == '=') {
S.Next();
tok = NEQ;
} else {
tok = NOT;
} }
return tok0;
}
case ';':
S.Next();
tok = SEMICOLON;
case '(':
S.Next();
tok = LPAREN;
case ')':
S.Next();
tok = LPAREN;
case '[':
S.Next();
tok = LBRACK;
case ']':
S.Next();
tok = RBRACK;
case '{': func (S *Scanner) Scan () (tok, beg, end int) {
S.Next(); S.SkipWhitespace();
tok = LBRACE;
case '}': var tok int = ILLEGAL;
S.Next(); var beg int = S.pos - 1;
tok = RBRACE; var end int = beg;
case '&': switch ch := S.ch; {
S.Next(); case is_letter(ch): tok = S.ScanIdentifier();
if S.ch == '&' { case is_dec_digit(ch): tok = S.ScanNumber();
default:
S.Next(); S.Next();
tok = AND; switch ch {
case -1: tok = EOF;
case '"': tok = S.ScanString();
case '\'': tok = S.ScanChar();
case '`': tok = S.ScanRawString();
case ':': tok = S.Select2(COLON, DEFINE);
case '.':
if is_dec_digit(S.ch) {
tok = S.ScanNumber();
} else { } else {
tok = BAND; tok = PERIOD;
} }
case ',': tok = COMMA;
case '|': case ';': tok = SEMICOLON;
S.Next(); case '(': tok = LPAREN;
if S.ch == '|' { case ')': tok = RPAREN;
S.Next(); case '[': tok = LBRACK;
tok = OR; case ']': tok = RBRACK;
} else { case '{': tok = LBRACE;
tok = BOR; case '}': tok = RBRACE;
case '+': tok = S.Select3(ADD, ADD_ASSIGN, '+', INC);
case '-': tok = S.Select3(SUB, SUB_ASSIGN, '-', DEC);
case '*': tok = S.Select2(MUL, MUL_ASSIGN);
case '/':
if S.ch == '/' || S.ch == '*' {
S.SkipComment();
// cannot simply return because of 6g bug
tok, beg, end = S.Scan();
return tok, beg, end;
} }
tok = S.Select2(QUO, QUO_ASSIGN);
case '%': tok = S.Select2(REM, REM_ASSIGN);
case '^': tok = S.Select2(XOR, XOR_ASSIGN);
case '<': tok = S.Select4(LSS, LEQ, '<', SHL, SHL_ASSIGN);
case '>': tok = S.Select4(GTR, GEQ, '>', SHR, SHR_ASSIGN);
case '=': tok = S.Select2(ASSIGN, EQL);
case '!': tok = S.Select2(NOT, NEQ);
case '&': tok = S.Select3(AND, AND_ASSIGN, '&', CAND);
case '|': tok = S.Select3(OR, OR_ASSIGN, '|', COR);
default: tok = ILLEGAL;
default:
S.Next(); // make progress
} }
} }
......
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