2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * If you wish your version of this file to be governed by only the CDDL
25 * or only the GPL Version 2, indicate your decision by adding
26 * "[Contributor] elects to include this software in this distribution
27 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28 * single choice of license, a recipient has the option to distribute
29 * your version of this file under either the CDDL, the GPL Version 2 or
30 * to extend the choice of license to its licensees as provided above.
31 * However, if you add GPL Version 2 code and therefore, elected the GPL
32 * Version 2 license, then the option applies only if the new code is
33 * made subject to such option by the copyright holder.
37 * Portions Copyrighted 2008 Sun Microsystems, Inc.
41 * Definition of Scala Grammar Definition.
43 * @author Caoyuan Deng
45 module org.netbeans.modules.scala.editing.rats.ParserScala;
47 import org.netbeans.modules.scala.editing.rats.Character;
48 import org.netbeans.modules.scala.editing.rats.Identifier;
49 import org.netbeans.modules.scala.editing.rats.Keyword;
50 import org.netbeans.modules.scala.editing.rats.Literal;
51 import org.netbeans.modules.scala.editing.rats.Spacing;
52 import org.netbeans.modules.scala.editing.rats.Symbol;
53 import org.netbeans.modules.scala.editing.rats.Xml;
55 import org.netbeans.modules.scala.editing.rats.State;
57 modify org.netbeans.modules.scala.editing.rats.Xml;
61 /* @Note: Modified productions should always be put at the beginning */
63 // ----- Modify XmlContent in Xml.rats
69 // ----- Modify XmlAttValue in Xml.rats
70 generic XmlAttValue +=
72 / <ScalaExpr> ScalaExpr
75 // ----- Modify XmlCharData in Xml.rats
76 generic XmlCharData := xmlCharDataInScala;
78 /* @Note: If we modify xmlCharDataChar in Xml.rats, we cannot add transient */
79 transient String xmlCharDataInScala = xmlCharDataCharInScala+;
80 transient String xmlCharDataCharInScala =
83 / !( [<&] / "]]>" / [{}] ) _
87 /* @Note: We cannot consumer every token's tail whitespace (leave it for XmlExpr
88 * to detect a preceding whitespace, instead, we consumer every token's preceding
89 * whitespace and ignore it.
93 / void:"(":sep XmlElement void:")":sep
94 / void:"{":sep XmlElement void:"}":sep
101 / void:"(":sep XmlElementP void:")":sep
102 / void:"{":sep XmlElementP void:"}":sep
105 generic XmlContentP =
106 <ScalaPatterns> ScalaPatterns
107 / <XmlCDSect> XmlCDSect
109 / <XmlCharData> XmlCharData
110 / <XmlElement> XmlElementP
111 / <XmlReference> XmlReference
112 / <XmlComment> XmlComment
115 generic XmlElementP =
117 / XmlSTagP XmlContentP* XmlETag
120 transient generic XmlEmptyElemTagP = XmlLt XmlEmptyTagName XmlWs? XmlSlashGt ;
121 transient generic XmlSTagP = XmlLt XmlSTagName XmlWs? XmlGt ;
123 /* @DocError in ScalaReference: ScalaExpr ::= '{' expr '}' */
124 transient generic ScalaExpr = void:"{":sep !( void:"{":sep ) Block void:"}":sep !( void:"}":sep );
125 transient generic ScalaPatterns = void:"{":sep !( void:"{":sep ) Patterns void:"}":sep !( void:"}":sep );
131 SKIP ( "-":op )? FloatingPointLiteral
132 / SKIP ( "-":op )? IntegerLiteral
133 / SKIP BooleanLiteral
135 / SKIP CharacterLiteral
140 transient generic BooleanLiteral = "true":key / "false":key;
141 transient generic NullLiteral = "null" ;
143 transient void SEMI = void:";":sep N* / N+ ;
145 transient generic QualId = Id ( void:".":sep Id )* ;
146 transient generic Ids = Id ( void:",":sep Id )* ;
150 / ( Id void:".":sep )? ThisKey
154 ( Id void:".":sep )? ThisKey ( void:".":sep Id )* ( void:"." SKIP ErrorIdExpected )?
155 / ( Id void:".":sep )? SuperKey ClassQualifier? ( void:".":sep Id )* ( void:"." SKIP ErrorIdExpected )?
156 / Id ( void:".":sep Id )* ( void:"." SKIP ErrorIdExpected )?
159 /* StableId may be followed by '{' '_' in Import, and "type", so should considerate this case */
160 transient generic ErrorIdExpected = !( [{_] / "type":key ) { yyValue = GNode.create("Error", "Id expected"); } ;
162 transient generic ClassQualifier = void:"[":sep Id void:"]":sep;
170 void:"(":sep ( void:"=>":key Type )? void:")":sep void:"=>":key Type @CallByNameFunType
171 / InfixType void:"=>":key Type @NormalFunType
174 /* in CaseClause, we should leave the latest "=>" for ( "=>" Block ) */
175 generic FunTypeInCaseClause =
176 void:"(":sep ( void:"=>":key Type )? void:")":sep void:"=>":key ( NotFunType void:"=>":key )*
177 / InfixType void:"=>":key ( NotFunType void:"=>":key )*
180 transient generic NotFunType = InfixType ExistentialClause? ;
182 transient generic ExistentialClause = "forSome":key void:"{":sep ExistentialDcl ( SEMI ExistentialDcl )* void:"}":sep;
184 transient generic ExistentialDcl =
185 void:"type":key TypeDcl
186 / void:"val" :key ValDcl
189 generic InfixType = CompoundType InfixTypeRest* ;
190 generic InfixTypeRest = Id N? CompoundType ;
192 generic CompoundType =
193 AnnotType ( void:"with":key AnnotType )* Refinement?
197 generic AnnotType = Annotation* SimpleType ;
200 Path void:".":sep void:"type":key TypeArgs? ( void:"#":key Id )* @SimpleSingletonType
201 / StableId TypeArgs? ( void:"#":key Id )* @SimpleIdType
202 / void:"(":sep Types ( void:",":sep )? void:")":sep TypeArgs? ( void:"#":key Id )* @SimpleTupleType
205 transient generic TypeArgs = void:"[":sep Types void:"]":sep ;
207 /* @DocError in ScalaReference: Type in Types can be "_" */
208 transient generic Types = ( Type / WildKey) ( void:",":sep ( Type / WildKey) )* ;
210 transient generic Refinement = N? void:"{":sep RefineStat? ( SEMI RefineStat? )* void:"}":sep ;
213 void:"type":key TypeDef
218 void:":":key CompoundType
219 / void:":":key Annotation Annotation*
220 / void:":":key "_":key "*":op
224 ( Bindings / Id ) void:"=>":key Expr
228 void:"if" :key void:"(":sep ENTER_NL_DISABLE Expr EXIT_NL_DISABLE void:")":sep N* Expr ( SEMI? void:"else":key Expr )? @IfExpr
229 / void:"while" :key void:"(":sep ENTER_NL_DISABLE Expr EXIT_NL_DISABLE void:")":sep N* Expr @WhileExpr
230 / void:"try" :key void:"{":sep Block void:"}":sep ( void:"catch":key void:"{":sep CaseClauses void:"}":sep )? ( void:"finally":key Expr )? @TryExpr
231 / void:"do" :key Expr SEMI? void:"while":key void:"(":sep ENTER_NL_DISABLE Expr EXIT_NL_DISABLE void:")":sep @DoExpr
232 / void:"for" :key ( void:"(":sep Enumerators void:")":sep / void:"{":sep Enumerators void:"}":sep ) N* ( void:"yield":key )? Expr @ForExpr
233 / void:"throw" :key Expr @ThrowExpr
234 / void:"return":key Expr? @ReturnExpr
235 / SimpleExpr void:"=":key Expr @AssignmentExpr
236 / PostfixExpr Ascription @AscriptionExpr
237 / PostfixExpr void:"match":key void:"{":sep CaseClauses void:"}":sep @MatchExpr
242 generic AssignmentExpr =
243 ( SimpleExpr void:".":sep )? Id void:"=":key Expr
244 / SimpleExpr ArgumentExprs void:"=":key Expr
248 /* @DocError in ScalaReference: InfixExpr ( Id N? )? */
249 generic PostfixExpr = InfixExpr Id? ;
251 generic InfixExpr = PrefixExpr InfixExprRest* ;
252 generic InfixExprRest = Id N? PrefixExpr;
254 generic PrefixExpr = ( "-":op / "+":op / "~":op / "!":op )? SimpleExpr ;
256 /* @DocError in ScalaReference : There is non SimpleExpr with ( void:":":key Type ) */
258 XmlExpr TypeArgs? Member* ( void:"." SKIP ErrorIdExpected )?
259 / Literal TypeArgs? Member* ( void:"." SKIP ErrorIdExpected )?
260 / Path TypeArgs? ArgumentExprs? Member* ( void:"." SKIP ErrorIdExpected )? ( void:":":key Type )?
261 / WildKey TypeArgs? Member* ( void:"." SKIP ErrorIdExpected )?
262 / ParenExpr TypeArgs? Member* ( void:"." SKIP ErrorIdExpected )? ( void:":":key Type )?
263 / BlockExpr TypeArgs? Member* ( void:"." SKIP ErrorIdExpected )?
264 / NewExpr TypeArgs? Member* ( void:"." SKIP ErrorIdExpected )?
268 void:".":sep Id TypeArgs? ArgumentExprs?
269 / ArgumentExprs // apply
272 transient generic NewExpr =
273 void:"new":key ( ClassTemplate / TemplateBody)
274 / SKIP void:"new" SKIP ErrorClassTemplateExpected
277 transient generic ErrorClassTemplateExpected = { yyValue = GNode.create("Error", "ClassTemplate expected"); } ;
279 transient generic Exprs = Expr ( void:",":sep Expr )*;
281 generic ArgumentExprs =
287 void:"(":sep ENTER_NL_DISABLE
288 ( Exprs ( void:",":sep )? )?
289 EXIT_NL_DISABLE void:")":sep
293 void:"{":sep CaseClauses void:"}":sep
294 / void:"{":sep Block void:"}":sep
299 ( BlockStat SEMI )* ResultExpr?
303 transient generic BlockStat =
305 / ( "implicit":key / "lazy":key )* Def
306 / LocalModifier* TmplDef
307 / !ResultFunExpr NotFunExpr
316 generic ResultFunExpr = ( Bindings / ResultBinding ) void:"=>":key Block ;
317 /* @DocError in ScalaReference: Id void:":":key CompoundType */
318 generic ResultBinding = Id ( void:":":key CompoundType )? ;
320 transient generic Enumerators = Generator ( SEMI Enumerator )*;
322 transient generic Enumerator =
325 / void:"val":key Pattern1 void:"=":key Expr
328 /* @Note "val" is deprecated */
329 transient generic Generator = ( void:"val":key )? Pattern1 void:"<-":key Expr Guard? ;
331 transient generic CaseClauses = CaseClause CaseClause* ;
332 /* @Hacking greedy Pattern will consumer latest "=>" to try to recognize as FunType,
333 * we should leave lastest "=>" for Block by define a FunTypeInCaseClause
335 * A case token can begin a statement only if followed by a class or object token
336 * @Todo nl in case clause
339 SKIP_N void:"case":key ENTER_NL_DISABLE
340 ( ( VarId / "_":key ) void:":" :key FunTypeInCaseClause EXIT_NL_DISABLE Block
341 / Pattern N* Guard? void:"=>":key EXIT_NL_DISABLE Block
345 transient generic Guard = void:"if":key PostfixExpr ;
352 transient generic AlternatePattern = Pattern1 void:"|":sep Pattern1 ( void:"|":sep Pattern1 )* ;
359 generic TypedPattern =
360 VarId void:":":key Type
361 / "_":key void:":":key Type
365 generic NoTypedPattern =
370 generic AtPattern = VarId void:"@":key Pattern3 ;
373 SimplePattern InfixPatternTail InfixPatternTail*
376 transient generic InfixPatternTail = Id N? SimplePattern ;
378 /* @DocError in ScalaReference: "_":key */
379 generic SimplePattern =
380 XmlPattern @SimpleXmlPattern
381 / "_":key ( "*":op )? @SimpleWildcardPattern
382 / Literal @SimpleLiteralPattern
383 / TuplePattern @SimpleTuplePattern
384 / StableId TuplePattern @SimpleCallPattern
385 / StableId @SimpleIdPattern
388 transient generic TuplePattern = void:"(":sep Patterns? void:")":sep ;
390 transient generic Patterns =
391 Pattern ( void:",":sep Pattern )* ( ",":sep "_":key "*":op / ",":sep )?
395 transient generic TypeParamClause = void:"[":sep VariantTypeParam ( void:",":sep VariantTypeParam )* void:"]":sep ;
397 transient generic FunTypeParamClause = void:"[":sep TypeParam ( void:",":sep TypeParam )* void:"]":sep ;
399 transient generic VariantTypeParam = ( "+":op / "-":op )? TypeParam ;
401 transient generic TypeParam = ( Id / WildKey ) TypeParamClause? ( void:">:":key Type )? ( void:"<:":key Type )? ( void:"<%":key Type )? ;
403 transient generic ParamClauses = ParamClause* ( N? void:"(":sep void:"implicit":key Params void:")":sep )? ;
405 transient generic ParamClause = N? void:"(":sep Params? void:")":sep ;
407 transient generic Params = Param ( void:",":sep Param )* ;
409 transient generic Param = Annotation* Id ( void:":":key ParamType )? ;
411 transient generic ParamType =
417 transient generic ClassParamClauses = ClassParamClause* ( N? void:"(":sep void:"implicit":key ClassParams void:")":sep )? ;
419 transient generic ClassParamClause = N? void:"(":sep ClassParams? void:")":sep ;
421 transient generic ClassParams = ClassParam ( void:",":sep ClassParam )* ;
423 transient generic ClassParam = Annotation* ( Modifier* ( void:"val":key / void:"var":key ) )? Id ( void:":":key ParamType )? ;
425 /* @DocError in ScalaReference: void:"(":sep Binding ( void:",":sep Binding )* void:")":sep */
426 transient generic Bindings = void:"(":sep Binding? ( void:",":sep Binding )* void:")":sep ;
427 transient generic Binding = Id ( void:":":key Type )? ;
435 generic LocalModifier =
443 transient generic AccessModifier = ( "private":key / "protected":key ) AccessQualifier? ;
445 transient generic AccessQualifier = void:"[":sep ( Id / ThisKey ) void:"]":sep ;
447 generic Annotation = "@":key AnnotationExpr N? ;
449 /* @DocError in ScalaReference: AnnotationExpr = Constr ( N? void:"{":sep NameValuePair* void:"}":sep )? */
450 generic AnnotationExpr = Constr ( N? void:"{":sep NameValuePairs* void:"}":sep )?;
452 generic NameValuePairs = NameValuePair ( void:",":sep NameValuePair )* ;
453 generic NameValuePair = void:"val":key Id void:"=":key PrefixExpr;
455 generic TemplateBody = N? void:"{":sep SelfType? TemplateStat ( SEMI TemplateStat )* void:"}":sep ;
457 generic TemplateStat =
459 / Annotation* Modifier* Def
460 / Annotation* Modifier* Dcl
466 Id ( void:":":key Type )? void:"=>":key
467 / ThisKey void:":":key Type void:"=>":key
470 transient generic Import =
471 void:"import":key ImportExpr ( void:",":sep ImportExpr )*
472 / void:"import" SKIP ErrorStableIdExpected
475 transient generic ErrorStableIdExpected = { yyValue = GNode.create("Error", "StableId expected"); } ;
477 transient generic ImportExpr = StableId ( void:".":sep ( WildKey / ImportSelectors ) )?;
479 transient generic ImportSelectors = void:"{":sep ( ImportSelector void:",":sep )* ( ImportSelector / WildKey ) void:"}":sep ;
481 transient generic ImportSelector = Id ( void:"=>":key ( Id / WildKey ) )? ;
484 void:"val" :key ValDcl
485 / void:"var" :key VarDcl
486 / void:"def" :key FunDcl
487 / void:"type":key N* TypeDcl
490 transient generic ValDcl = Ids void:":":key Type ;
491 transient generic VarDcl = Ids void:":":key Type ;
492 transient generic FunDcl = FunSig ( void:":":key Type )? ;
494 transient generic FunSig = Id FunTypeParamClause? ParamClauses ;
496 transient generic TypeDcl = Id TypeParamClause? ( void:">:":key Type )? ( void:"<:":key Type )? ;
499 void:"val":key ValDef
500 / void:"var":key VarDef
505 / void:"def" :key FunDef
506 / void:"type":key N* TypeDef
510 transient generic PatDef = NoTypedPattern ( void:",":sep NoTypedPattern )* ( void:":":key Type )? void:"=":key Expr ;
512 transient generic ValDef = PatDef ;
516 / Ids void:":":key Type "=":key "_":key
520 FunSig ( void:":":key Type )? void:"=":key Expr
521 / FunSig N? void:"{":sep Block void:"}":sep
522 / ThisKey ParamClause ParamClauses ( void:"=":key ConstrExpr / N? ConstrBlock ) @ConstructorFunDef
525 generic TypeDef = Id TypeParamClause? void:"=":key Type ;
528 ( "case":key )? void:"class" :key ClassDef
529 / ( "case":key )? void:"object":key ObjectDef
530 / void:"trait" :key TraitDef
533 transient generic ClassDef = Id TypeParamClause? Annotation* AccessModifier? ClassParamClauses ClassTemplateOpt ;
534 transient generic TraitDef = Id TypeParamClause? TraitTemplateOpt ;
535 transient generic ObjectDef = Id ClassTemplateOpt ;
537 generic ClassTemplateOpt =
538 Extends ClassTemplate
539 / Extends? TemplateBody
543 generic TraitTemplateOpt =
544 Extends TraitTemplate
545 / Extends? TemplateBody
549 generic Extends = "extends":key / "<:":key ;
551 transient generic ClassTemplate = ( void:"{":sep EarlyDefs? void:"}":sep void:"with":key )? ClassParents TemplateBody? ;
552 transient generic TraitTemplate = ( void:"{":sep EarlyDefs? void:"}":sep void:"with":key )? TraitParents TemplateBody? ;
554 transient generic ClassParents = Constr ( void:"with":key AnnotType )* ;
555 transient generic TraitParents = AnnotType ( void:"with":key AnnotType )* ;
557 transient generic Constr = AnnotType ArgumentExprs* ;
559 transient generic EarlyDefs = EarlyDef ( SEMI EarlyDef )* ;
560 transient generic EarlyDef = Annotation* Modifier* PatVarDef ;
567 transient generic ConstrBlock = void:"{":sep SelfInvocation ( SEMI BlockStat )* void:"}":sep ;
569 transient generic SelfInvocation = ThisKey ArgumentExprs ArgumentExprs* ;
571 transient generic TopStatSeq = TopStat ( SEMI TopStat )* ;
576 / Annotation* Modifier* TmplDef
580 transient generic Packaging = void:"package":key QualId N? void:"{":sep TopStatSeq void:"}":sep ;
581 transient generic Package = void:"package":key QualId SEMI !( "{":sep );
583 public generic CompilationUnit = SKIP_N Package? TopStatSeq EOF;