Commit a4564638 authored by Robert Griesemer's avatar Robert Griesemer

- changed my scanner/parser to accept new channel syntax

R=r
OCL=15439
CL=15439
parent f7a506bf
...@@ -56,8 +56,8 @@ type ( ...@@ -56,8 +56,8 @@ type (
export type M5 (p T5) . (a, b int, c float) (z T5, ok bool); export type M5 (p T5) . (a, b int, c float) (z T5, ok bool);
type T6 chan int type T6 chan int
type T7 chan<- *T6 type T7 <- chan *T6
type T8 chan-< *T6 type T8 chan <- *T6
type T9 struct { type T9 struct {
p *T9; p *T9;
......
...@@ -430,8 +430,8 @@ func (P *Parser) ParseArrayType() *Globals.Type { ...@@ -430,8 +430,8 @@ func (P *Parser) ParseArrayType() *Globals.Type {
} }
P.Expect(Scanner.RBRACK); P.Expect(Scanner.RBRACK);
typ.elt = P.ParseVarType(); typ.elt = P.ParseVarType();
P.Ecart();
P.Ecart();
return typ; return typ;
} }
...@@ -439,21 +439,23 @@ func (P *Parser) ParseArrayType() *Globals.Type { ...@@ -439,21 +439,23 @@ func (P *Parser) ParseArrayType() *Globals.Type {
func (P *Parser) ParseChannelType() *Globals.Type { func (P *Parser) ParseChannelType() *Globals.Type {
P.Trace("ChannelType"); P.Trace("ChannelType");
P.Expect(Scanner.CHAN);
typ := Globals.NewType(Type.CHANNEL); typ := Globals.NewType(Type.CHANNEL);
switch P.tok { if P.tok == Scanner.CHAN {
case Scanner.SEND:
typ.flags = Type.SEND;
P.Next(); P.Next();
case Scanner.RECV: if P.tok == Scanner.ARROW {
typ.flags = Type.RECV; typ.flags = Type.SEND;
P.Next(); P.Next();
default: } else {
typ.flags = Type.SEND + Type.RECV; typ.flags = Type.SEND + Type.RECV;
} }
} else {
P.Expect(Scanner.ARROW);
P.Expect(Scanner.CHAN);
typ.flags = Type.RECV;
}
typ.elt = P.ParseVarType(); typ.elt = P.ParseVarType();
P.Ecart();
P.Ecart();
return typ; return typ;
} }
...@@ -797,7 +799,7 @@ func (P *Parser) TryType() *Globals.Type { ...@@ -797,7 +799,7 @@ func (P *Parser) TryType() *Globals.Type {
switch P.tok { switch P.tok {
case Scanner.IDENT: typ = P.ParseTypeName(); case Scanner.IDENT: typ = P.ParseTypeName();
case Scanner.LBRACK: typ = P.ParseArrayType(); case Scanner.LBRACK: typ = P.ParseArrayType();
case Scanner.CHAN: typ = P.ParseChannelType(); case Scanner.CHAN, Scanner.ARROW: typ = P.ParseChannelType();
case Scanner.INTERFACE: typ = P.ParseInterfaceType(); case Scanner.INTERFACE: typ = P.ParseInterfaceType();
case Scanner.LPAREN: typ = P.ParseFunctionType(); case Scanner.LPAREN: typ = P.ParseFunctionType();
case Scanner.MAP: typ = P.ParseMapType(); case Scanner.MAP: typ = P.ParseMapType();
...@@ -946,25 +948,14 @@ func (P *Parser) ParseExpressionPairList(list *Globals.List) { ...@@ -946,25 +948,14 @@ func (P *Parser) ParseExpressionPairList(list *Globals.List) {
func (P *Parser) ParseCompositeLit(typ *Globals.Type) Globals.Expr { func (P *Parser) ParseCompositeLit(typ *Globals.Type) Globals.Expr {
P.Trace("CompositeLit"); P.Trace("CompositeLit");
// TODO I think we should use {} instead of () for
// composite literals to syntactically distinguish
// them from conversions. For now: allow both.
var paren int;
if P.tok == Scanner.LPAREN {
P.Next();
paren = Scanner.RPAREN;
} else {
P.Expect(Scanner.LBRACE); P.Expect(Scanner.LBRACE);
paren = Scanner.RBRACE;
}
// TODO: should allow trailing ',' // TODO: should allow trailing ','
list := Globals.NewList(); list := Globals.NewList();
if P.tok != paren { if P.tok != Scanner.RBRACE {
list.AddExpr(P.ParseExpression()); list.AddExpr(P.ParseExpression());
if P.tok == Scanner.COMMA { if P.tok == Scanner.COMMA {
P.Next(); P.Next();
if P.tok != paren { if P.tok != Scanner.RBRACE {
P.ParseExpressionList(list); P.ParseExpressionList(list);
} }
} else if P.tok == Scanner.COLON { } else if P.tok == Scanner.COLON {
...@@ -972,14 +963,13 @@ func (P *Parser) ParseCompositeLit(typ *Globals.Type) Globals.Expr { ...@@ -972,14 +963,13 @@ func (P *Parser) ParseCompositeLit(typ *Globals.Type) Globals.Expr {
list.AddExpr(P.ParseExpression()); list.AddExpr(P.ParseExpression());
if P.tok == Scanner.COMMA { if P.tok == Scanner.COMMA {
P.Next(); P.Next();
if P.tok != paren { if P.tok != Scanner.RBRACE {
P.ParseExpressionPairList(list); P.ParseExpressionPairList(list);
} }
} }
} }
} }
P.Expect(Scanner.RBRACE);
P.Expect(paren);
P.Ecart(); P.Ecart();
return nil; return nil;
...@@ -1228,7 +1218,7 @@ func (P *Parser) ParseUnaryExpr() Globals.Expr { ...@@ -1228,7 +1218,7 @@ func (P *Parser) ParseUnaryExpr() Globals.Expr {
case Scanner.NOT: fallthrough; case Scanner.NOT: fallthrough;
case Scanner.XOR: fallthrough; case Scanner.XOR: fallthrough;
case Scanner.MUL: fallthrough; case Scanner.MUL: fallthrough;
case Scanner.RECV: fallthrough; case Scanner.ARROW: fallthrough;
case Scanner.AND: case Scanner.AND:
P.Next(); P.Next();
P.ParseUnaryExpr(); P.ParseUnaryExpr();
...@@ -1249,7 +1239,7 @@ func Precedence(tok int) int { ...@@ -1249,7 +1239,7 @@ func Precedence(tok int) int {
return 1; return 1;
case Scanner.LAND: case Scanner.LAND:
return 2; return 2;
case Scanner.SEND, Scanner.RECV: case Scanner.ARROW:
return 3; return 3;
case Scanner.EQL, Scanner.NEQ, Scanner.LSS, Scanner.LEQ, Scanner.GTR, Scanner.GEQ: case Scanner.EQL, Scanner.NEQ, Scanner.LSS, Scanner.LEQ, Scanner.GTR, Scanner.GEQ:
return 4; return 4;
...@@ -1702,7 +1692,7 @@ func (P *Parser) TryStatement() bool { ...@@ -1702,7 +1692,7 @@ func (P *Parser) TryStatement() bool {
case Scanner.FUNC: case Scanner.FUNC:
// for now we do not allow local function declarations // for now we do not allow local function declarations
fallthrough; fallthrough;
case Scanner.MUL, Scanner.SEND, Scanner.RECV, Scanner.IDENT, Scanner.LPAREN: case Scanner.MUL, Scanner.ARROW, Scanner.IDENT, Scanner.LPAREN:
P.ParseSimpleStat(); P.ParseSimpleStat();
case Scanner.GO: case Scanner.GO:
P.ParseGoStat(); P.ParseGoStat();
......
...@@ -54,8 +54,7 @@ export const ( ...@@ -54,8 +54,7 @@ export const (
SHL; SHL;
SHR; SHR;
SEND; ARROW;
RECV;
ADD_ASSIGN; ADD_ASSIGN;
SUB_ASSIGN; SUB_ASSIGN;
...@@ -163,8 +162,7 @@ export func TokenName(tok int) string { ...@@ -163,8 +162,7 @@ export func TokenName(tok int) string {
case SHL: return "<<"; case SHL: return "<<";
case SHR: return ">>"; case SHR: return ">>";
case SEND: return "-<"; case ARROW: return "<-";
case RECV: return "<-";
case ADD_ASSIGN: return "+="; case ADD_ASSIGN: return "+=";
case SUB_ASSIGN: return "-="; case SUB_ASSIGN: return "-=";
...@@ -740,13 +738,7 @@ func (S *Scanner) Scan() (tok, pos int, val string) { ...@@ -740,13 +738,7 @@ func (S *Scanner) Scan() (tok, pos int, val string) {
case '{': tok = LBRACE; case '{': tok = LBRACE;
case '}': tok = RBRACE; case '}': tok = RBRACE;
case '+': tok = S.Select3(ADD, ADD_ASSIGN, '+', INC); case '+': tok = S.Select3(ADD, ADD_ASSIGN, '+', INC);
case '-': case '-': tok = S.Select3(SUB, SUB_ASSIGN, '-', DEC);
if S.ch == '<' {
S.Next();
tok = SEND;
} else {
tok = S.Select3(SUB, SUB_ASSIGN, '-', DEC);
}
case '*': tok = S.Select2(MUL, MUL_ASSIGN); case '*': tok = S.Select2(MUL, MUL_ASSIGN);
case '/': case '/':
if S.ch == '/' || S.ch == '*' { if S.ch == '/' || S.ch == '*' {
...@@ -761,7 +753,7 @@ func (S *Scanner) Scan() (tok, pos int, val string) { ...@@ -761,7 +753,7 @@ func (S *Scanner) Scan() (tok, pos int, val string) {
case '<': case '<':
if S.ch == '-' { if S.ch == '-' {
S.Next(); S.Next();
tok = RECV; tok = ARROW;
} else { } else {
tok = S.Select4(LSS, LEQ, '<', SHL, SHL_ASSIGN); tok = S.Select4(LSS, LEQ, '<', SHL, SHL_ASSIGN);
} }
...@@ -791,7 +783,7 @@ func (S *Scanner) Server(c *chan *Token) { ...@@ -791,7 +783,7 @@ func (S *Scanner) Server(c *chan *Token) {
for { for {
t := new(Token); t := new(Token);
t.tok, t.pos, t.val = S.Scan(); t.tok, t.pos, t.val = S.Scan();
c -< t; c <- t;
if t.tok == EOF { if t.tok == EOF {
break; break;
} }
......
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