lang: Add resource specific tokens in lexer and parser

This adds some custom tokens for the lexer and parser so that resources
can have colons in their names.
This commit is contained in:
James Shubin
2018-07-07 17:12:37 -04:00
parent 86b8099eb9
commit a26620da38
2 changed files with 29 additions and 3 deletions

View File

@@ -330,6 +330,20 @@
lval.str = s[1:len(s)] // remove the leading $ lval.str = s[1:len(s)] // remove the leading $
return VAR_IDENTIFIER return VAR_IDENTIFIER
} }
/[A-Z][a-z0-9:]*[a-z0-9]*/
{
yylex.pos(lval) // our pos
s := yylex.Text()
lval.str = strings.ToLower(s) // uncapitalize it
return CAPITALIZED_RES_IDENTIFIER
}
/[a-z][a-z0-9:]*[a-z0-9]*/
{
yylex.pos(lval) // our pos
s := yylex.Text()
lval.str = strings.ToLower(s) // uncapitalize it
return RES_IDENTIFIER
}
/[A-Z][a-z0-9]*/ /[A-Z][a-z0-9]*/
{ {
yylex.pos(lval) // our pos yylex.pos(lval) // our pos

View File

@@ -85,6 +85,7 @@ func init() {
%token STR_IDENTIFIER BOOL_IDENTIFIER INT_IDENTIFIER FLOAT_IDENTIFIER %token STR_IDENTIFIER BOOL_IDENTIFIER INT_IDENTIFIER FLOAT_IDENTIFIER
%token MAP_IDENTIFIER STRUCT_IDENTIFIER VARIANT_IDENTIFIER VAR_IDENTIFIER IDENTIFIER %token MAP_IDENTIFIER STRUCT_IDENTIFIER VARIANT_IDENTIFIER VAR_IDENTIFIER IDENTIFIER
%token VAR_IDENTIFIER_HX CAPITALIZED_IDENTIFIER %token VAR_IDENTIFIER_HX CAPITALIZED_IDENTIFIER
%token RES_IDENTIFIER CAPITALIZED_RES_IDENTIFIER
%token CLASS_IDENTIFIER INCLUDE_IDENTIFIER %token CLASS_IDENTIFIER INCLUDE_IDENTIFIER
%token COMMENT ERROR %token COMMENT ERROR
@@ -743,7 +744,8 @@ rbind:
; ;
*/ */
resource: resource:
IDENTIFIER expr OPEN_CURLY resource_body CLOSE_CURLY // `file "/tmp/hello" { ... }` or `aws:ec2 "/tmp/hello" { ... }`
RES_IDENTIFIER expr OPEN_CURLY resource_body CLOSE_CURLY
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.stmt = &StmtRes{ $$.stmt = &StmtRes{
@@ -752,6 +754,16 @@ resource:
Contents: $4.resContents, Contents: $4.resContents,
} }
} }
// TODO: do we need to include this simpler case as well?
//| IDENTIFIER expr OPEN_CURLY resource_body CLOSE_CURLY
// {
// posLast(yylex, yyDollar) // our pos
// $$.stmt = &StmtRes{
// Kind: $1.str,
// Name: $2.expr,
// Contents: $4.resContents,
// }
// }
; ;
resource_body: resource_body:
/* end of list */ /* end of list */
@@ -864,7 +876,7 @@ edge_half_list:
; ;
edge_half: edge_half:
// eg: Test["t1"] // eg: Test["t1"]
CAPITALIZED_IDENTIFIER OPEN_BRACK expr CLOSE_BRACK CAPITALIZED_RES_IDENTIFIER OPEN_BRACK expr CLOSE_BRACK
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.edgeHalf = &StmtEdgeHalf{ $$.edgeHalf = &StmtEdgeHalf{
@@ -876,7 +888,7 @@ edge_half:
; ;
edge_half_sendrecv: edge_half_sendrecv:
// eg: Test["t1"].foo_send // eg: Test["t1"].foo_send
CAPITALIZED_IDENTIFIER OPEN_BRACK expr CLOSE_BRACK DOT IDENTIFIER CAPITALIZED_RES_IDENTIFIER OPEN_BRACK expr CLOSE_BRACK DOT IDENTIFIER
{ {
posLast(yylex, yyDollar) // our pos posLast(yylex, yyDollar) // our pos
$$.edgeHalf = &StmtEdgeHalf{ $$.edgeHalf = &StmtEdgeHalf{