root/trunk/ProjectFortress/src/com/sun/fortress/parser/Literal.rats @ 2737

Revision 2737, 6.5 KB (checked in by sukyoungryu, 15 months ago)

[parser] Numerals contain no more than one '.' character.

Line 
1/*******************************************************************************
2    Copyright 2008 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18/*
19 * Definition of Fortress literals.
20 */
21module com.sun.fortress.parser.Literal(MayNewlineHeader, DelimitedExpr,
22                                       NoSpaceExpr, Symbol, Spacing,
23                                       Identifier);
24
25import MayNewlineHeader;
26import DelimitedExpr;
27import NoSpaceExpr;
28import Symbol;
29import Spacing;
30import Identifier;
31
32/* LiteralExpr ::=
33     ( w )
34   | NumericLiteralExpr
35   | CharLiteralExpr
36   | StringLiteralExpr
37 */
38Expr LiteralExpr =
39     <VOID> VoidLiteralExpr
40   / <NUMERICAL> NumericLiteralExpr
41   / <CHAR>CharLiteralExpr
42   / <STRING> StringLiteralExpr
43   ;
44
45/* ArrayExpr ::= [ StaticArgs? w RectElements w ] */
46ArrayElements ArrayExpr =
47     void:opensquare a1:StaticArgs? w a2:RectElements w void:closesquare
48     { if (a1 == null) yyValue = FortressUtil.finalizeArrayExpr(a2);
49       else            yyValue = FortressUtil.addStaticArgsToArrayExpr(a1, a2);
50     };
51
52/* RectElements ::= NoSpaceExpr MultiDimCons* */
53private ArrayElements RectElements =
54     a1:NoSpaceExpr a2s:MultiDimCons*
55     { if (a2s == null || a2s.isEmpty()) {
56             List<ArrayExpr> list = new ArrayList<ArrayExpr>();
57             list.add(new ArrayElement(a1.getSpan(), false, a1));
58             yyValue = new ArrayElements(a1.getSpan(), false, 1, list);
59         } else
60           yyValue = FortressUtil.multiDimCons(a1, a2s.list());
61     };
62
63/* MultiDimCons ::= RectSeparator NoSpaceExpr */
64com.sun.fortress.useful.Pair<Integer,Expr> MultiDimCons =
65     a1:RectSeparator a2:NoSpaceExpr
66     { yyValue = new com.sun.fortress.useful.Pair<Integer,Expr>(a1,a2); };
67
68Expr VoidLiteralExpr =
69     <FIRST> openparen w closeparen
70     { yyValue = ExprFactory.makeVoidLiteralExpr(createSpan(yyStart,yyCount)); };
71
72NumberLiteralExpr NumericLiteralExpr =
73    a1:NumericWord a2s:RestNumericWord+ "_" a3:RadixSpecifier &{ NodeUtil.validNumericLiteral(a1, a2s.list(), a3) }
74    { String numeral = a1;
75      for (String numericWord: a2s.list()) {
76          numeral += numericWord;
77      }
78      yyValue = ExprFactory.makeFloatLiteralExpr(createSpan(yyStart,yyCount),
79                                                 numeral+"_"+NodeUtil.radix2Number(a3));
80    }
81  / a1:NumericWord a2s:RestNumericWord+ &{ NodeUtil.validNumericLiteral(a1, a2s.size()) }
82    { String numeral = a1;
83      for (String numericWord: a2s.list()) {
84          numeral += numericWord;
85      }
86      yyValue = ExprFactory.makeFloatLiteralExpr(createSpan(yyStart,yyCount), numeral);
87    }
88  / FloatLiteralExpr
89  / IntLiteralExpr ;
90
91private transient String RestNumericWord =
92    a1:NumericSeparator a2:NumericWord { yyValue = a1+a2; };
93
94transient FloatLiteralExpr FloatLiteralExpr =
95    <ErrorProduction> DigitString dot DigitString (dot DigitString)+
96    { yyValue = syntaxError(createSpan(yyStart,yyCount),
97                            "Numerals contain no more than one `.' character.");
98    }
99  / <FIRST> a1:DigitString dot a2:DigitString
100    { yyValue = ExprFactory.makeFloatLiteralExpr(createSpan(yyStart,yyCount),
101                                                 a1 + "." + a2);
102    };
103
104transient IntLiteralExpr IntLiteralExpr =
105    <FIRST> a1:NumericWord "_" a2:RadixSpecifier &{ NodeUtil.validNumericLiteral(a1, a2) }
106    { Span span = createSpan(yyStart,yyCount);
107      yyValue = ExprFactory.makeIntLiteralExpr(span, a1+"_"+NodeUtil.radix2Number(a2));
108    }
109  / a1:NumericWord &{ NodeUtil.validNumericLiteral(a1) }
110    { yyValue = ExprFactory.makeIntLiteralExpr(createSpan(yyStart,yyCount), a1); }
111  / a1:DigitString
112    { yyValue = ExprFactory.makeIntLiteralExpr(createSpan(yyStart,yyCount),
113                                               a1);
114    };
115
116private transient String NumericCharacter = [0-9a-zA-Z];
117private transient String NumericWord =
118    a1s:NumericCharacter+ a2s:(NumericSpace / NumericCharacter)* &{ NodeUtil.validNumericWord(a2s.list()) }
119    { yyValue = "";
120      for (String n: a1s.list()) {
121          yyValue += n;
122      }
123      for (String n: a2s.list()) {
124          yyValue += n;
125      }
126    };
127
128private transient String NumericSpace = "'" / "\u202f" ;
129private transient String NumericSeparator = "." ;
130
131private transient String RadixSpecifier =
132     RadixDigits
133   / RadixNames ;
134
135private transient String RadixDigits =
136     "16"
137   / "15"
138   / "14"
139   / "13"
140   / "12"
141   / "11"
142   / "10"
143   / "9"
144   / "8"
145   / "7"
146   / "6"
147   / "5"
148   / "4"
149   / "3"
150   / "2" ;
151
152private transient String RadixNames =
153     "SIXTEEN"
154   / "FIFTEEN"
155   / "FOURTEEN"
156   / "THIRTEEN"
157   / "TWELVE"
158   / "ELEVEN"
159   / "TEN"
160   / "NINE"
161   / "EIGHT"
162   / "SEVEN"
163   / "SIX"
164   / "FIVE"
165   / "FOUR"
166   / "THREE"
167   / "TWO" ;
168
169private transient String DigitString = [0-9]+;
170
171CharLiteralExpr CharLiteralExpr =
172     <FIRST> "'" a1:CharLiteralContent "'"
173     { yyValue = ExprFactory.makeCharLiteralExpr(createSpan(yyStart,yyCount),
174                                                 a1);
175     };
176
177StringLiteralExpr StringLiteralExpr =
178     <FIRST> ["] a1:StringLiteralContent* ["]
179     { String str = "";
180       for (String c : (List<String>)a1.list()) {
181           str = str.concat(c);
182       }
183       yyValue = new StringLiteralExpr(createSpan(yyStart,yyCount), false, str);
184     };
185private String StringLiteralContent =
186     EscapeSequence
187   / a1:(!["\\] _) { yyValue = String.valueOf(a1); };
188
189private String EscapeSequence = '\\' a1:[btnfr"\\]
190     { switch (a1) {
191           case 'b': { yyValue = "\b"; break; }
192           case 't': { yyValue = "\t"; break; }
193           case 'n': { yyValue = "\n"; break; }
194           case 'f': { yyValue = "\f"; break; }
195           case 'r': { yyValue = "\r"; break; }
196           case '"': { yyValue = "\""; break; }
197           case '\'': { yyValue = "\'"; break; }
198           case '\\': { yyValue = "\\"; break; }
199           default: { yyValue = ""; }
200       }
201     };
202
203private String CharLiteralContent =
204     EscapeSequence
205   / a1:(![\\] _) { yyValue = String.valueOf(a1); };
Note: See TracBrowser for help on using the browser.