[Swift-commit] r6384 - trunk/docs/merged/refmanual
ketan at ci.uchicago.edu
ketan at ci.uchicago.edu
Fri Mar 15 16:53:56 CDT 2013
Author: ketan
Date: 2013-03-15 16:53:56 -0500 (Fri, 15 Mar 2013)
New Revision: 6384
Added:
trunk/docs/merged/refmanual/grammar
Modified:
trunk/docs/merged/refmanual/build.sh
trunk/docs/merged/refmanual/swiftlang
Log:
first cut at swift grammar, needs refinement, and crystallization
Modified: trunk/docs/merged/refmanual/build.sh
===================================================================
--- trunk/docs/merged/refmanual/build.sh 2013-03-15 15:02:00 UTC (rev 6383)
+++ trunk/docs/merged/refmanual/build.sh 2013-03-15 21:53:56 UTC (rev 6384)
@@ -1,3 +1,3 @@
#!/bin/sh
-asciidoc --attribute stylesheet=${PWD}/swift.css swiftlang
+asciidoc --attribute stylesheet=${PWD}/swift.css refmanual.txt
Added: trunk/docs/merged/refmanual/grammar
===================================================================
--- trunk/docs/merged/refmanual/grammar (rev 0)
+++ trunk/docs/merged/refmanual/grammar 2013-03-15 21:53:56 UTC (rev 6384)
@@ -0,0 +1,1381 @@
+== Swift Language Grammar
+
+The specification for a SwiftScript program
+----
+program:
+ (namespace_dec[code])*
+ (importStatement[code])*
+ (topLevelStatement[code])*
+ EOF
+ ;
+----
+
+Namespace Declaration
+----
+namespace_dec [StringTemplate code]
+{StringTemplate ns=template("nsDef");}:
+"namespace" (prefix:ID{ns.setAttribute("prefix", prefix.getText());})? uri:STRING_LITERAL SEMI {
+ ns.setAttribute("uri", uri.getText());
+ code.setAttribute("namespaces", ns);
+ if (ns.getAttribute("prefix") == null)
+ code.setAttribute("targetNS", ns.getAttribute("uri"));
+ }
+ ;
+----
+
+
+Import statement
+----
+importStatement [StringTemplate code] :
+"import" name:STRING_LITERAL SEMI {
+ StringTemplate i = template("import");
+ i.setAttribute("target", name.getText());
+ code.setAttribute("imports", i);
+ }
+ ;
+----
+
+Type Declaration
+----
+typedecl [StringTemplate code]
+{StringTemplate r=template("typeDef");
+ StringTemplate t=null;} :
+ "type" id:ID {
+ r.setAttribute("name", id.getText()); }
+ (
+ SEMI
+ |
+ (t=type
+ {
+ r.setAttribute("type", t);
+ }
+ SEMI
+ )
+ | structdecl[r]
+ )
+ {code.setAttribute("types", r);}
+ ;
+----
+
+Structure declaration
+----
+structdecl [StringTemplate code]
+{StringTemplate e=null, e1=null, t=null; String thisType = null;} :
+ LCURLY
+ (t=type id:ID
+ {
+ thisType = (String) t.getAttribute("name");
+ e=template("memberdefinition");
+ e.setAttribute("name", id.getText());
+ }
+ (LBRACK RBRACK { thisType = thisType + "[]"; })*
+ {
+ StringTemplate thisTypeTemplate;
+ thisTypeTemplate=template("type");
+ thisTypeTemplate.setAttribute("name", thisType);
+ e.setAttribute("type", thisTypeTemplate);
+ code.setAttribute("members", e);
+ }
+ (
+ COMMA
+ id1:ID
+ {
+ thisType = (String) t.getAttribute("name");
+ e1=template("memberdefinition");
+ e1.setAttribute("name", id1.getText());
+ }
+ (LBRACK RBRACK { thisType = thisType + "[]"; })*
+ {
+ StringTemplate thisTypeTemplate;
+ thisTypeTemplate=template("type");
+ thisTypeTemplate.setAttribute("name", thisType);
+ e1.setAttribute("type", thisTypeTemplate);
+ code.setAttribute("members", e1);
+ }
+ )*
+ SEMI
+ )*
+ RCURLY
+ (options {
+ warnWhenFollowAmbig = false;
+ }
+ :SEMI
+ )?
+ ;
+----
+
+Top level statement
+----
+topLevelStatement[StringTemplate code]
+{StringTemplate d=null; } :
+
+// these are ll(1) and easy to predict
+
+ typedecl[code]
+ | d=ll1statement
+ {
+ code.setAttribute("statements",d);
+ }
+
+// these are non-declaration assign-like statements
+
+ | (predictAssignStat) => d=assignStat
+ {
+ code.setAttribute("statements",d);
+ }
+
+// these are non-declaration append-associative array statements
+
+ | (predictAppendStat) => d=appendStat
+ {
+ code.setAttribute("statements",d);
+ }
+
+// they all begin with (id name)
+ | (predictDeclaration) => declaration[code]
+
+// more complicated function invocations
+// note that function invocations can happen in above statements too
+// this section is just the remaining more specialised invocations
+
+ | (procedurecallCode) => d=procedurecallCode
+ {
+ code.setAttribute("statements",d);
+ }
+
+ | (procedurecallStatAssignManyReturnParam[code]) => procedurecallStatAssignManyReturnParam[code]
+
+// this is a declaration, but not sorted out the predications yet to
+// group it into a decl block
+ | ("app") => d=appproceduredecl {code.setAttribute("functions",d);}
+ | (predictProceduredecl) => d=proceduredecl {code.setAttribute("functions", d);}
+ ;
+----
+
+
+Predicate Declaration
+----
+predictDeclaration {StringTemplate x,y;} : ("global") | (x=type y=declarator) ;
+
+declaration [StringTemplate code]
+{StringTemplate t=null;
+ boolean isGlobal = false;}
+ : ("global" {isGlobal = true;})?
+ t=type
+ declpart[code, t, isGlobal]
+ (COMMA declpart[code, t, isGlobal])*
+ SEMI
+ ;
+----
+
+Declaration part
+----
+declpart [StringTemplate code, StringTemplate t, boolean isGlobal]
+ {
+ StringTemplate n=null;
+ StringTemplate thisTypeTemplate=null;
+ String thisType = (String) t.getAttribute("name");
+ StringTemplate variable=null;
+ StringTemplate m = null;
+ StringTemplate sTemp = null;
+ String sType = "";
+ } :
+ n=declarator
+ (LBRACK
+ (sTemp=type {sType = (String) sTemp.getAttribute("name") ;} )?
+ RBRACK {thisType = thisType + "[" + sType + "]" ; sType = ""; } )*
+ {
+ thisTypeTemplate=template("type");
+ thisTypeTemplate.setAttribute("name", thisType);
+ variable = template("variable");
+ variable.setAttribute("name", n);
+ variable.setAttribute("type", thisTypeTemplate);
+ variable.setAttribute("global", ""+isGlobal);
+ code.setAttribute("statements", variable);
+ }
+
+ (LT (m=mappingdecl | f:STRING_LITERAL) GT
+ {
+ if (m!=null)
+ variable.setAttribute("mapping", m);
+ else
+ variable.setAttribute("lfn", quote(f.getText()));
+ })?
+
+// TODO: mapping does here...
+// which means construction of the variable template goes here, rather than
+// in procedurecallDecl/variableDecl
+
+ (
+ (predictProcedurecallDecl) => procedurecallDecl[code, thisTypeTemplate, n, variable]
+ | variableDecl[code, thisTypeTemplate, n, variable]
+// nice to lose this distinction entirely...
+// | (predictDatasetdecl) => datasetdecl[code, thisTypeTemplate, n]
+// TODO can shorten variableDecl predictor now we dont' need to
+// distinguish it from datasetdecl?
+ )
+ ;
+----
+
+Variable Declarator
+----
+variableDecl [StringTemplate code, StringTemplate t, StringTemplate d, StringTemplate v1]
+{StringTemplate i1=null, m=null;
+
+} :
+ (i1=varInitializer
+ {
+ if (i1 != null) {
+ StringTemplate valueAssignment = template("assign");
+ StringTemplate vr = template("variableReference");
+ vr.setAttribute("name",d);
+ valueAssignment.setAttribute("lhs",vr);
+ valueAssignment.setAttribute("rhs",i1);
+ code.setAttribute("statements", valueAssignment);
+ }
+ })?
+ ;
+----
+
+Declarator
+----
+declarator returns [StringTemplate code=null] :
+id:ID {code=text(id.getText());}
+ ;
+----
+
+
+Variable initializer
+----
+varInitializer returns [StringTemplate code=null] :
+ASSIGN code=expression
+ ;
+----
+
+This is an initializer used to set up an array.
+ currently does not support nested array.
+----
+arrayInitializer returns [StringTemplate code=template("arrayInit")]
+{StringTemplate e=null,from=null,to=null,step=null;} :
+LBRACK
+ (
+ (expression COLON) =>
+ (
+ from=expression COLON to=expression (COLON step=expression)?
+ {
+ StringTemplate range=template("range");
+ range.setAttribute("from", from);
+ range.setAttribute("to", to);
+ if (step != null)
+ range.setAttribute("step", step);
+ code.setAttribute("range", range);
+ }
+ )
+ |
+ (
+ e=expression {code.setAttribute("elements", e);}
+ (
+ // CONFLICT: does a COMMA after an initializer start a new
+ // initializer or start the option ',' at end?
+ // ANTLR generates proper code by matching
+ // the comma as soon as possible.
+ options {
+ warnWhenFollowAmbig = false;
+ } : COMMA e=expression {code.setAttribute("elements", e);}
+ )*
+ (COMMA)?
+ )
+ )?
+ RBRACK
+ ;
+----
+
+Mapping Declaration
+----
+mappingdecl returns [StringTemplate code=template("mapping")]
+{StringTemplate p=null, d=null;}
+ : d=declarator {code.setAttribute("descriptor",d);} SEMI
+ mapparamdecl[code]
+ ;
+----
+
+Mapping parameters declaration
+----
+mapparamdecl [StringTemplate code]
+{StringTemplate p=null;} :
+( p=mapparam {code.setAttribute("params", p);}
+ ( COMMA p=mapparam {code.setAttribute("params", p);} )*
+ )?
+ ;
+
+mapparam returns [StringTemplate code=template("mapParam")]
+{StringTemplate n=null, v=null;} :
+n=declarator ASSIGN v=mappingExpr
+ {
+ code.setAttribute("name", n);
+ code.setAttribute("value", v);
+ }
+ ;
+----
+
+This predicts in two different ways.
+The first choice is procedures with no return parameters. For these,
+we must predict as far as the opening { in order to distinguish
+from procedure calls to procedures with no return parameters.
+The second choice is for procedures with return parameters. Here we
+predict as far as the bracket after the procedure name. We have to
+predict on the return parameters, which means we won't get good
+error reporting when there is a syntax error in there.
+
+----
+predictProceduredecl
+{StringTemplate f=null;} :
+( id:ID LPAREN
+ ( f=formalParameter (COMMA f=formalParameter)* )?
+ RPAREN
+ LCURLY
+ )
+ |
+ (
+ LPAREN
+ f=formalParameter
+ ( COMMA f=formalParameter
+ )*
+ RPAREN ID LPAREN
+ )
+ ;
+----
+
+Procedure declaration
+----
+proceduredecl returns [StringTemplate code=template("function")]
+{StringTemplate f=null;} :
+( LPAREN
+ f=formalParameter
+ {
+ f.setAttribute("outlink", "true");
+ code.setAttribute("outputs", f);
+ }
+ ( COMMA f=formalParameter
+ {
+ f.setAttribute("outlink", "true");
+ code.setAttribute("outputs", f);
+ }
+ )*
+ RPAREN )?
+ id:ID {currentFunctionName=id.getText();} LPAREN
+ ( f=formalParameter
+ {
+ code.setAttribute("inputs", f);
+ }
+ ( COMMA f=formalParameter
+ {
+ code.setAttribute("inputs", f);
+ }
+ )*
+ )?
+ RPAREN
+ LCURLY
+ (
+ atomicBody[code]
+ |
+ compoundBody[code]
+ )
+ RCURLY
+ {
+ code.setAttribute("name", id.getText());
+ currentFunctionName=null;
+ }
+ ;
+----
+
+App procedure declaration
+----
+appproceduredecl returns [StringTemplate code=template("function")]
+{StringTemplate f=null;
+ StringTemplate app=template("app");
+ StringTemplate exec=null; } :
+ "app"
+ ( LPAREN
+ f=formalParameter
+ {
+ f.setAttribute("outlink", "true");
+ code.setAttribute("outputs", f);
+ }
+ ( COMMA f=formalParameter
+ {
+ f.setAttribute("outlink", "true");
+ code.setAttribute("outputs", f);
+ }
+ )*
+ RPAREN )?
+ id:ID {currentFunctionName=id.getText();} LPAREN
+ ( f=formalParameter
+ {
+ code.setAttribute("inputs", f);
+ }
+ ( COMMA f=formalParameter
+ {
+ code.setAttribute("inputs", f);
+ }
+ )*
+ )?
+ RPAREN
+ LCURLY
+ ( appProfile[app] )*
+ exec=declarator
+ {app.setAttribute("exec",exec);}
+ ( appArg[app] )* SEMI
+ {code.setAttribute("config",app);}
+ RCURLY
+ {
+ code.setAttribute("name", id.getText());
+ currentFunctionName=null;
+ }
+ ;
+----
+
+App profile
+----
+appProfile [StringTemplate code]
+{ StringTemplate p=null;
+ StringTemplate k=null;
+ StringTemplate v=null;} :
+ "profile" k=expression ASSIGN v=expression SEMI {
+ p=template("app_profile");
+ p.setAttribute("key", k);
+ p.setAttribute("value", v);
+ code.setAttribute("profiles", p);
+ }
+ ;
+----
+
+TODO in here, why do we have an | between LBRACKBRACK and ASSIGN?
+does this mean that we don't have array initialisation in formal
+params? this wouldn't surprise me given the previous treatment
+of arrays. investigate this and fix...
+
+----
+formalParameter returns [StringTemplate code=template("parameter")]
+{StringTemplate t=null,d=null,v=null; String thisType = null; } :
+(t=type d=declarator
+ {
+ thisType = (String) t.getAttribute("name");
+ code.setAttribute("name", d);
+ }
+ (LBRACK RBRACK {thisType = thisType + "[]"; })*
+ (ASSIGN v=constant
+ {
+ String value = (String)v.getAttribute("value");
+ if (v.getName().equals("sConst")) {
+ v.removeAttribute("value");
+ v.setAttribute("value", quote(value));
+ }
+ code.setAttribute("defaultv", v);
+ }
+ )?) {
+ StringTemplate thisTypeTemplate;
+ thisTypeTemplate=template("type");
+ thisTypeTemplate.setAttribute("name", thisType);
+ code.setAttribute("type", thisTypeTemplate);
+ }
+ ;
+----
+
+Type declaration
+----
+type returns [ StringTemplate code = null ]
+ { StringBuilder buf = new StringBuilder(); } :
+ id:ID {
+ code = template("type");
+ buf.append(id.getText());
+ }
+ (typeSubscript[buf]) * {
+ code.setAttribute("name", buf.toString());
+ }
+;
+----
+
+Type Subscript
+----
+typeSubscript[StringBuilder buf] :
+ LBRACK { buf.append('['); }
+ (id:ID { buf.append(id.getText()); })?
+ RBRACK { buf.append(']'); }
+;
+----
+
+Compound Statement
+----
+compoundStat[StringTemplate code]
+ : LCURLY
+ ( innerStatement[code] )*
+ RCURLY
+ ;
+----
+
+Compound body
+----
+compoundBody[StringTemplate code]
+ : ( innerStatement[code] )*
+ ;
+
+innerStatement[StringTemplate code]
+{StringTemplate s=null;}
+ : (predictDeclaration) => declaration[code]
+ |
+ ((
+ s=ll1statement
+ | (procedurecallCode) => s=procedurecallCode
+ | (predictAssignStat) => s=assignStat
+ | (predictAppendStat) => s=appendStat
+ )
+ {
+ code.setAttribute("statements",s);
+ })
+ | (procedurecallStatAssignManyReturnParam[code]) => procedurecallStatAssignManyReturnParam[code]
+ ;
+----
+
+Case Inner Statement
+----
+caseInnerStatement [StringTemplate statements]
+{ StringTemplate code = null; }
+ :
+ ( code=ll1statement
+ | (procedurecallCode) => code=procedurecallCode
+ | (predictAssignStat) => code=assignStat
+ | (predictAppendStat) => code=appendStat
+ ) {statements.setAttribute("statements",code);}
+ | (procedurecallStatAssignManyReturnParam[statements]) => procedurecallStatAssignManyReturnParam[statements]
+ ;
+----
+
+These are the statements that we can predict with ll(1) grammer
+i.e. with one token of lookahead
+
+----
+ll1statement returns [StringTemplate code=null]
+ :
+ code=ifStat
+ | code=foreachStat
+ | code=switchStat
+ | code=iterateStat
+ ;
+----
+If statement
+----
+ifStat returns [StringTemplate code=template("if")]
+{
+ StringTemplate cond=null;
+ StringTemplate body=template("statementList");
+ StringTemplate els=template("statementList");
+}
+ : "if" LPAREN cond=expression RPAREN {
+ code.setAttribute("cond", cond);
+ }
+ compoundStat[body] {code.setAttribute("body", body);}
+ (
+ options {
+ warnWhenFollowAmbig = false;
+ }
+ : "else"
+ compoundStat[els] {code.setAttribute("els", els);}
+ )?
+ ;
+----
+
+Foreach statement
+----
+foreachStat returns [StringTemplate code=template("foreach")] {
+ StringTemplate ds=null;
+ StringTemplate body=template("statementList");
+}
+ : "foreach" id:ID (COMMA indexId:ID)? "in" ds=expression {
+ code.setAttribute("var", id.getText());
+ code.setAttribute("in", ds);
+ if (indexId != null) {
+ code.setAttribute("index", indexId.getText());
+ }
+ }
+ compoundStat[body] {code.setAttribute("body", body);}
+ ;
+----
+
+Iterate statement
+----
+iterateStat returns [StringTemplate code=template("iterate")]
+{
+ StringTemplate cond=null;
+ StringTemplate body=template("statementList");
+}
+ : "iterate" id:ID
+ compoundStat[body] {code.setAttribute("body", body);}
+ "until" LPAREN cond=expression RPAREN SEMI
+ {
+ code.setAttribute("var", id.getText());
+ code.setAttribute("cond", cond);
+ }
+ ;
+----
+
+Switch statement
+
+----
+switchStat returns [StringTemplate code=template("switch")]
+{
+ StringTemplate cond=null, b=null;
+}
+ : "switch" LPAREN cond=expression RPAREN
+ {code.setAttribute("cond", cond);}
+ LCURLY
+ ( b = casesGroup {code.setAttribute("cases", b);} )*
+ RCURLY
+ ;
+----
+
+Cases Group
+----
+casesGroup returns [StringTemplate code=template("case")]
+{StringTemplate b=null;}
+ : ( // CONFLICT: to which case group do the statements bind?
+ // ANTLR generates proper code: it groups the
+ // many "case"/"default" labels together then
+ // follows them with the statements
+ options {
+ greedy = true;
+ }
+ :
+ aCase[code]
+ )
+ caseSList[code]
+ ;
+
+aCase [StringTemplate code]
+{StringTemplate v=null;}
+ : (
+ "case" v=expression {code.setAttribute("value", v);}
+ | "default"
+ )
+ COLON
+ ;
+----
+
+Case Slist
+----
+caseSList [StringTemplate code]
+{StringTemplate s=null;}
+ : ( caseInnerStatement[code] )*
+ ;
+----
+
+Predict assignment statement
+----
+predictAssignStat
+{StringTemplate x=null;}
+ : x=identifier ASSIGN ;
+----
+
+Predict append statement
+----
+predictAppendStat
+{StringTemplate x=null;}
+ : x=identifier APPEND ;
+----
+
+Assignment statement
+----
+assignStat returns [StringTemplate code=null]
+{StringTemplate id=null;}
+ :
+ id=identifier
+ ASSIGN
+ (
+ (predictProcedurecallAssign) => code=procedurecallCode
+ { StringTemplate o = template("returnParam");
+ o.setAttribute("name",id);
+ code.setAttribute("outputs",o);
+ }
+ |
+ code=variableAssign
+ {
+ code.setAttribute("lhs",id);
+ }
+ )
+ ;
+----
+
+Append statement
+----
+appendStat returns [ StringTemplate code = null ]
+ { StringTemplate id=null; }
+:
+ id=identifier
+ APPEND
+ (
+ (predictProcedurecallAssign) => code=procedurecallCode {
+ StringTemplate o = template("returnParam");
+ o.setAttribute("name",id);
+ code.setAttribute("outputs",o);
+ }
+ |
+ code=arrayAppend {
+ code.setAttribute("array", id);
+ }
+ )
+;
+----
+
+Array append statement
+----
+arrayAppend returns [ StringTemplate code = null ]
+ { StringTemplate a = null, e = null, id = null; }
+:
+ e = expression SEMI {
+ code = template("append");
+ code.setAttribute("value", e);
+ }
+;
+----
+
+Variable Assign statement
+----
+variableAssign returns [StringTemplate code=null]
+{StringTemplate a=null, e=null, id=null;}
+ :
+ e=expression SEMI {
+ code=template("assign");
+ code.setAttribute("rhs", e);
+ }
+ ;
+----
+
+Procedure call
+----
+procedurecallCode returns [StringTemplate code=template("call")]
+{StringTemplate f=null;}
+ :
+ procedureInvocation[code]
+ ;
+----
+
+procedure invocation
+----
+procedureInvocation [StringTemplate code]
+ :
+ procedureInvocationWithoutSemi[code]
+ SEMI
+ ;
+----
+
+procedure invocation without semicolon
+----
+procedureInvocationWithoutSemi [StringTemplate code]
+{StringTemplate f=null;}
+ :
+ id:ID {code.setAttribute("func", id.getText());}
+ LPAREN
+ ( f=actualParameter {
+ code.setAttribute("inputs", f);
+ }
+ ( COMMA f=actualParameter {
+ code.setAttribute("inputs", f);
+ }
+ )*
+ )?
+ RPAREN
+ ;
+----
+
+procedure invocation expression
+----
+procedureInvocationExpr [StringTemplate code]
+{StringTemplate f=null;}
+ :
+ id:ID {code.setAttribute("func", id.getText());}
+ LPAREN (
+ f=actualParameter {
+ code.setAttribute("inputs", f);
+ }
+ (
+ COMMA f=actualParameter {
+ code.setAttribute("inputs", f);
+ }
+ )*
+ )?
+ RPAREN
+ ;
+----
+
+procedure call expression
+----
+procedureCallExpr returns [StringTemplate code=template("call")]
+{StringTemplate f=null;}
+ :
+ procedureInvocationExpr[code]
+ ;
+----
+
+
+----
+predictProcedurecallDecl : ASSIGN ID LPAREN ;
+
+procedurecallDecl [StringTemplate container, StringTemplate type, StringTemplate decl, StringTemplate var]
+{
+StringTemplate code=template("call");
+StringTemplate f=template("returnParam");
+
+StringTemplate declref=template("variableReference");
+declref.setAttribute("name",decl);
+f.setAttribute("name", declref);
+code.setAttribute("outputs", f);
+container.setAttribute("statements",code);
+
+}
+ :
+ ASSIGN
+ procedureInvocationWithoutSemi[code]
+ ;
+----
+
+----
+procedurecallStatAssignManyReturnParam [StringTemplate s]
+{ StringTemplate code=template("call"); }
+ :
+ LPAREN
+ procedurecallStatAssignManyReturnOutput[s,code]
+ ( COMMA procedurecallStatAssignManyReturnOutput[s,code] )*
+ RPAREN
+ ASSIGN
+ procedureInvocation[code]
+ {
+ s.setAttribute("statements",code);
+ }
+ ;
+----
+
+----
+procedurecallStatAssignManyReturnOutput [StringTemplate s, StringTemplate code]
+{StringTemplate var = null, f = null; }
+ :
+ f=returnParameter
+ {
+ code.setAttribute("outputs", f);
+ var = template("variable");
+ if(f.getAttribute("type") != null) {
+ StringTemplate nameST = (StringTemplate)f.getAttribute("name");
+ var.setAttribute("name",nameST.getAttribute("name"));
+ var.setAttribute("type",f.getAttribute("type"));
+ var.setAttribute("global", Boolean.FALSE);
+
+ s.setAttribute("statements",var);
+ }
+ }
+ ;
+----
+
+----
+returnParameter returns [ StringTemplate code = template("returnParam") ]
+ { StringTemplate t = null, id = null, d = null; StringBuilder buf = new StringBuilder(); }
+:
+ t = identifier { buf.append(t.getAttribute("name")); }
+ (typeSubscript[buf])*
+ ( id = identifier )?
+ {
+ if (id == null) {
+ code.setAttribute("name", t);
+ }
+ else {
+ t = template("type");
+ t.setAttribute("name", buf.toString());
+ code.setAttribute("name", id);
+ code.setAttribute("type", t);
+ }
+ }
+ ((ASSIGN declarator) => (ASSIGN d=declarator) {
+ code.setAttribute("bind", d);
+ })?
+;
+----
+
+----
+actualParameter returns [StringTemplate code=template("actualParam")]
+{StringTemplate d=null, id=null, ai=null;}
+ :
+ (
+ (declarator ASSIGN)=> (d=declarator ASSIGN)
+ {
+ code.setAttribute("bind", d);
+ }
+ )?
+ id=expression {
+ code.setAttribute("value", id);
+ }
+ ;
+----
+
+----
+atomicBody [StringTemplate code]
+{StringTemplate app=null, svc=null;}
+ : app=appSpec
+ {code.setAttribute("config",app);}
+ ;
+----
+
+/* This is the deprecated format for app { } blocks */
+----
+appSpec returns [StringTemplate code=template("app")]
+{StringTemplate exec=null;}
+ : "app" LCURLY
+ exec=declarator
+ { code.setAttribute("exec", exec);}
+ (
+ appArg[code]
+ )*
+ SEMI RCURLY
+ ;
+----
+
+----
+appArg [StringTemplate code]
+{StringTemplate arg=null;}
+ : arg=mappingExpr
+ {code.setAttribute("arguments", arg);}
+ |
+ stdioArg[code]
+ ;
+----
+
+----
+mappingExpr returns [StringTemplate code=null]
+{StringTemplate e=null;}
+ :
+ e = expression
+ {
+ code=template("mappingExpr");
+ code.setAttribute("expr", e);
+ }
+ ;
+----
+
+----
+functionInvocation returns [StringTemplate code=template("functionInvocation")]
+{StringTemplate func=null, e=null;}
+ : AT (
+ (declarator LPAREN) =>
+ (func=declarator
+ {
+ code.setAttribute("name", func);
+ }
+ LPAREN
+ (
+ functionInvocationArgument[code]
+ (
+ COMMA
+ functionInvocationArgument[code]
+ )*)?
+ RPAREN
+ )
+ |
+ (e=identifier | (LPAREN e=identifier RPAREN) )
+ {
+ code.setAttribute("name", "filename");
+ code.setAttribute("args", e);
+ })
+ ;
+----
+
+----
+functionInvocationArgument [StringTemplate code]
+{StringTemplate e = null;}
+ :
+ e=expression
+ {
+ code.setAttribute("args", e);
+ }
+ ;
+----
+
+----
+stdioArg [StringTemplate code]
+{StringTemplate t=null,m=null; String name=null;}
+ : ("stdin" {t=template("stdin"); name="stdin";}
+ |
+ "stdout" {t=template("stdout"); name="stdout";}
+ |
+ "stderr" {t=template("stderr"); name="stderr";}
+ )
+ ASSIGN
+ m=mappingExpr
+ {
+ t.setAttribute("content", m);
+ code.setAttribute(name, t);
+ }
+ ;
+----
+
+----
+expression returns [StringTemplate code=null]
+ : code=orExpr
+ ;
+----
+
+----
+orExpr returns [StringTemplate code=null]
+{StringTemplate a,b;}
+ : code=andExpr
+ ( OR b=andExpr
+ {
+ a = code;
+ code=template("or");
+ code.setAttribute("left", a);
+ code.setAttribute("right", b);
+ }
+ )*
+ ;
+----
+
+----
+andExpr returns [StringTemplate code=null]
+{StringTemplate a,b;}
+ : code=equalExpr
+ ( AND b=equalExpr
+ {
+ a = code;
+ code=template("and");
+ code.setAttribute("left", a);
+ code.setAttribute("right", b);
+ }
+ )*
+ ;
+----
+
+----
+equalExpr returns [StringTemplate code=null]
+{
+StringTemplate a,b=null;
+Token op=null;
+}
+ : code=condExpr
+ (
+ {op=LT(1);}
+ ( EQ | NE ) b=condExpr
+ {
+ a = code;
+ code=template("cond");
+ code.setAttribute("op", escape(op.getText()));
+ code.setAttribute("left", a);
+ code.setAttribute("right", b);
+ }
+ )?
+ ;
+----
+
+----
+condExpr returns [StringTemplate code=null] {
+StringTemplate a,b=null;
+Token op=null;
+} : code=additiveExpr
+ (
+ options {
+ greedy = true;
+ //warnWhenFollowAmbig = false;
+ }
+ :
+ {op=LT(1);}
+ ( LT | LE | GT | GE ) b=additiveExpr
+ {
+ a = code;
+ code=template("cond");
+ code.setAttribute("op", escape(op.getText()));
+ code.setAttribute("left", a);
+ code.setAttribute("right", b);
+ }
+ )?
+ ;
+----
+
+
+----
+additiveExpr returns [StringTemplate code=null]
+{
+StringTemplate a,b=null;
+Token op=null;
+}
+ : code=multiExpr
+ (
+ options {
+ greedy = true;
+ //warnWhenFollowAmbig = false;
+ }
+ :
+ {op=LT(1);}
+ ( PLUS | MINUS ) b=multiExpr
+ {
+ a = code;
+ code=template("arith");
+ code.setAttribute("op", escape(op.getText()));
+ code.setAttribute("left", a);
+ code.setAttribute("right", b);
+ }
+ )*
+ ;
+----
+
+----
+multiExpr returns [StringTemplate code=null]
+{
+StringTemplate a,b=null;
+Token op=null;
+}
+ : code=unaryExpr
+ (
+ options {
+ greedy = true;
+ //warnWhenFollowAmbig = false;
+ }
+ :
+ {op=LT(1);}
+ ( STAR | IDIV | FDIV | MOD ) b=unaryExpr
+ {
+ a = code;
+ code=template("arith");
+ code.setAttribute("op", escape(op.getText()));
+ code.setAttribute("left", a);
+ code.setAttribute("right", b);
+ }
+ )*
+ ;
+----
+
+----
+unaryExpr returns [StringTemplate code=null]
+{StringTemplate u=null;}
+ : MINUS u=unaryExpr
+ {code=template("unaryNegation"); code.setAttribute("exp", u);}
+ | PLUS u=unaryExpr // unary plus has no effect
+ {code=u;}
+ | NOT u=unaryExpr
+ {code=template("not"); code.setAttribute("exp", u);}
+ | code=primExpr
+ ;
+----
+
+----
+primExpr returns [StringTemplate code=null]
+{StringTemplate id=null, exp=null;}
+ : (predictProcedureCallExpr) => code=procedureCallExpr
+ | code=identifier
+ | LPAREN exp=orExpr RPAREN { code=template("paren");
+ code.setAttribute("exp", exp);}
+ | code=constant
+ | code=functionInvocation
+ ;
+----
+
+----
+predictProcedureCallExpr
+ : ID LPAREN ;
+
+----
+// TODO - redo identifier parsing to fit in with the XML style
+// that this patch introduces
+
+// specifically, need the base ID to be distinct from all the
+// other IDs
+
+----
+identifier returns [StringTemplate code=null]
+{
+ StringTemplate c=null;
+ code=template("variableReference");
+}
+ :
+ base:ID {code.setAttribute("name",base.getText());}
+
+ ( ( c=arrayIndex { c.setAttribute("array",code); code=c; } )
+ |
+ ( c=memberName {c.setAttribute("structure",code); code=c;} )
+ )*
+ ;
+----
+
+
+----
+arrayIndex returns [StringTemplate code=null]
+{StringTemplate e=null;}
+ :
+ LBRACK
+ (e=expression | s:STAR)
+ RBRACK
+ {
+ code=template("arraySubscript");
+ if(e != null) code.setAttribute("subscript",e);
+ if(s != null) {
+ StringTemplate st = template("sConst");
+ st.setAttribute("value","*");
+ code.setAttribute("subscript",st);
+ }
+ }
+ ;
+----
+
+----
+memberName returns [StringTemplate code=null]
+ :
+ d:DOT (member:ID | s:STAR)
+ {
+ code=template("memberAccess");
+ if(member != null) code.setAttribute("name",member.getText());
+ if(s != null) code.setAttribute("name","*");
+ }
+ ;
+----
+
+----
+constant returns [StringTemplate code=null]
+ : i:INT_LITERAL
+ {
+ code=template("iConst");
+ code.setAttribute("value",i.getText());
+ }
+ | d:FLOAT_LITERAL
+ {
+ code=template("fConst");
+ code.setAttribute("value",d.getText());
+ }
+ | s:STRING_LITERAL
+ {
+ code=template("sConst");
+ code.setAttribute("value",quote(escape(s.getText())));
+ }
+ | t:"true"
+ {
+ code=template("bConst");
+ code.setAttribute("value", t.getText());
+ }
+ | f:"false"
+ {
+ code=template("bConst");
+ code.setAttribute("value", f.getText());
+ }
+ | code=arrayInitializer
+ ;
+----
+
+// TODO ^^^^^^ array literal -- rename and rearrange the methods
+
+
+----
+AT : "@" ;
+PLUS : "+" ;
+MINUS : '-' ;
+FDIV : '/' ;
+IDIV : "%/" ;
+MOD : "%%" ;
+EQ : "==" ;
+NE : "!=" ;
+LT : '<' ;
+LE : "<=" ;
+GT : ">" ;
+GE : ">=";
+APPEND : "<<";
+ASSIGN : '=' ;
+AND : "&&";
+OR : "||";
+NOT : "!";
+LBRACK options { paraphrase = "'['"; } : '[' ;
+RBRACK options { paraphrase = "']'"; } : ']' ;
+LPAREN options { paraphrase = "'('"; } : '(' ;
+RPAREN options { paraphrase = "')'"; } : ')' ;
+LCURLY options { paraphrase = "'{'"; } : '{' ;
+RCURLY options { paraphrase = "'}'"; } : '}' ;
+SEMI options { paraphrase = "a semicolon"; } : ';' ;
+
+----
+
+Id
+
+----
+ID options
+ {
+ paraphrase = "an identifier";
+ testLiterals = true;
+ }
+ :
+ ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
+ ;
+----
+
+----
+// string literals
+STRING_LITERAL
+// : '"'! (~('"'|'\n'|'\r'))* '"'!
+ : '"'! (escape_sequence|~('"'|'\\'|'\n'|'\r'))* '"'!
+ ;
+
+NUMBER
+ :
+ ( INTPART {_ttype=INT_LITERAL; }
+ ('.' FLOATPART {_ttype=FLOAT_LITERAL; })?
+ (EXPONENT {_ttype=FLOAT_LITERAL; })?
+ )
+ |
+ ( '.' { _ttype=DOT; }
+ ((FLOATPART {_ttype=FLOAT_LITERAL; })
+ (EXPONENT)?)?
+ )
+ ;
+
+
+whitespace : ( ' '
+ | '\t'
+ | '\r'
+ | '\n' {newline();}
+ )+
+ { $setType(Token.SKIP); }
+ ;
+
+singleline_ccomment:
+ "//"
+ (~('\n'|'\r'))* ('\n'|'\r'('\n')?)
+ {$setType(Token.SKIP); newline();}
+ ;
+
+singleline_scomment:
+ "#"
+ (~('\n'|'\r'))* ('\n'|'\r'('\n')?)
+ {$setType(Token.SKIP); newline();}
+ ;
+
+multiline_comment :
+ "/*"
+ (
+ options {
+ generateAmbigWarnings=false;
+ }
+ :
+ { LA(2)!='/' }? '*'
+ | '\r' '\n' {newline();}
+ | '\r' {newline();}
+ | '\n' {newline();}
+ | ~('*'|'\n'|'\r')
+ )*
+ "*/"
+ {$setType(Token.SKIP);}
+ ;
+
+escape_sequence :
+ '\\'
+ ( 'n'
+ | 'r'
+ | 't'
+ | 'b'
+ | 'f'
+ | '"'
+ | '\''
+ | '\\'
+ )
+ ;
+
+----
Modified: trunk/docs/merged/refmanual/swiftlang
===================================================================
--- trunk/docs/merged/refmanual/swiftlang 2013-03-15 15:02:00 UTC (rev 6383)
+++ trunk/docs/merged/refmanual/swiftlang 2013-03-15 21:53:56 UTC (rev 6384)
@@ -1,17 +1,5 @@
-////
-Swift Language Reference Manual, asciidoc format
-////
-
-:toc:
-:numbered:
-
-////
-Settings:
-////
-:miscellaneous.newline: \n
-
-= Swift Language Referance Manual
+== Swift Language Reference Manual
This reference manual semi-formally outlines the Swift language conventions
== Support
More information about the Swift-commit
mailing list