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

Revision 2598, 6.3 KB (checked in by sukyoungryu, 16 months ago)

[parser] Supports more Unicode characters in Fortress programs including oxford brackets.

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    <FIRST> a1:DigitString dot a2:DigitString
96    { yyValue = ExprFactory.makeFloatLiteralExpr(createSpan(yyStart,yyCount),
97                                                 a1 + "." + a2); };
98
99transient IntLiteralExpr IntLiteralExpr =
100    <FIRST> a1:NumericWord "_" a2:RadixSpecifier &{ NodeUtil.validNumericLiteral(a1, a2) }
101    { Span span = createSpan(yyStart,yyCount);
102      yyValue = ExprFactory.makeIntLiteralExpr(span, a1+"_"+NodeUtil.radix2Number(a2));
103    }
104  / a1:NumericWord &{ NodeUtil.validNumericLiteral(a1) }
105    { yyValue = ExprFactory.makeIntLiteralExpr(createSpan(yyStart,yyCount), a1); }
106  / a1:DigitString
107    { yyValue = ExprFactory.makeIntLiteralExpr(createSpan(yyStart,yyCount),
108                                               a1);
109    };
110
111private transient String NumericCharacter = [0-9a-zA-Z];
112private transient String NumericWord =
113    a1s:NumericCharacter+ a2s:(NumericSpace / NumericCharacter)* &{ NodeUtil.validNumericWord(a2s.list()) }
114    { yyValue = "";
115      for (String n: a1s.list()) {
116          yyValue += n;
117      }
118      for (String n: a2s.list()) {
119          yyValue += n;
120      }
121    };
122
123private transient String NumericSpace = "'" / "\u202f" ;
124private transient String NumericSeparator = "." ;
125
126private transient String RadixSpecifier =
127     RadixDigits
128   / RadixNames ;
129
130private transient String RadixDigits =
131     "16"
132   / "15"
133   / "14"
134   / "13"
135   / "12"
136   / "11"
137   / "10"
138   / "9"
139   / "8"
140   / "7"
141   / "6"
142   / "5"
143   / "4"
144   / "3"
145   / "2" ;
146
147private transient String RadixNames =
148     "SIXTEEN"
149   / "FIFTEEN"
150   / "FOURTEEN"
151   / "THIRTEEN"
152   / "TWELVE"
153   / "ELEVEN"
154   / "TEN"
155   / "NINE"
156   / "EIGHT"
157   / "SEVEN"
158   / "SIX"
159   / "FIVE"
160   / "FOUR"
161   / "THREE"
162   / "TWO" ;
163
164private transient String DigitString = [0-9]+;
165
166CharLiteralExpr CharLiteralExpr =
167     <FIRST> "'" a1:CharLiteralContent "'"
168     { yyValue = ExprFactory.makeCharLiteralExpr(createSpan(yyStart,yyCount),
169                                                 a1);
170     };
171
172StringLiteralExpr StringLiteralExpr =
173     <FIRST> ["] a1:StringLiteralContent* ["]
174     { String str = "";
175       for (String c : (List<String>)a1.list()) {
176           str = str.concat(c);
177       }
178       yyValue = new StringLiteralExpr(createSpan(yyStart,yyCount), false, str);
179     };
180private String StringLiteralContent =
181     EscapeSequence
182   / a1:(!["\\] _) { yyValue = String.valueOf(a1); };
183
184private String EscapeSequence = '\\' a1:[btnfr"\\]
185     { switch (a1) {
186           case 'b': { yyValue = "\b"; break; }
187           case 't': { yyValue = "\t"; break; }
188           case 'n': { yyValue = "\n"; break; }
189           case 'f': { yyValue = "\f"; break; }
190           case 'r': { yyValue = "\r"; break; }
191           case '"': { yyValue = "\""; break; }
192           case '\'': { yyValue = "\'"; break; }
193           case '\\': { yyValue = "\\"; break; }
194           default: { yyValue = ""; }
195       }
196     };
197
198private String CharLiteralContent =
199     EscapeSequence
200   / a1:(![\\] _) { yyValue = String.valueOf(a1); };
Note: See TracBrowser for help on using the browser.