Mercurial > hg > truffle
view truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SimpleLanguage.atg @ 22157:dc83cc1f94f2
Using fully qualified imports
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 16 Sep 2015 11:33:22 +0200 |
parents | c07e64ecb528 |
children |
line wrap: on
line source
/* * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * The Universal Permissive License (UPL), Version 1.0 * * Subject to the condition set forth below, permission is hereby granted to any * person obtaining a copy of this software, associated documentation and/or * data (collectively the "Software"), free of charge and under any and all * copyright rights in the Software, and any and all patent rights owned or * freely licensable by each licensor hereunder covering either (i) the * unmodified Software as contributed to or provided by such licensor, or (ii) * the Larger Works (as defined below), to deal in both * * (a) the Software, and * * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if * one is included with the Software each a "Larger Work" to which the Software * is contributed by such licensors), * * without restriction, including without limitation the rights to copy, create * derivative works of, display, perform, and distribute the Software and make, * use, sell, offer for sale, import, export, have made, and have sold the * Software and the Larger Work(s), and to sublicense the foregoing rights on * either these or other terms. * * This license is subject to the following condition: * * The above copyright notice and either this complete permission notice or at a * minimum a reference to the UPL must be included in all copies or substantial * portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /* * This is the grammar of SL that is used to automatically generate the Parser.java and Scanner.java * files. You can download the parser generator Coco/R from http://ssw.jku.at/coco/. Then run * "java -jar Coco.jar SimpleLanguage.atg" */ COMPILER SimpleLanguage CHARACTERS letter = 'A'..'Z' + 'a'..'z'. nonZeroDigit = "123456789". digit = "0123456789". cr = '\r'. lf = '\n'. tab = '\t'. stringChar = ANY - "\"" - '\\' - cr - lf. TOKENS identifier = letter {letter | digit}. stringLiteral = "\"" { stringChar } "\"". numericLiteral = "0" | nonZeroDigit { digit }. PRAGMAS COMMENTS FROM "/*" TO "*/" COMMENTS FROM "//" TO lf IGNORE cr + lf + tab PRODUCTIONS SimpleLanguage = Function { Function } . Function = "function" identifier (. Token identifierToken = t; .) "(" (. int bodyStartPos = t.charPos; .) (. factory.startFunction(identifierToken, bodyStartPos); .) [ identifier (. factory.addFormalParameter(t); .) { "," identifier (. factory.addFormalParameter(t); .) } ] ")" Block<out SLStatementNode body, false> (. factory.finishFunction(body); .) . Block<out SLStatementNode result, boolean inLoop> = (. factory.startBlock(); List<SLStatementNode> body = new ArrayList<>(); .) "{" (. int start = t.charPos; .) { Statement<out SLStatementNode s, inLoop> (. body.add(s); .) } "}" (. int length = (t.charPos + t.val.length()) - start; .) (. result = factory.finishBlock(body, start, length); .) . Statement<out SLStatementNode result, boolean inLoop> = (. result = null; .) ( WhileStatement<out result> | "break" (. if (inLoop) { result = factory.createBreak(t); } else { SemErr("break used outside of loop"); } .) ";" | "continue" (. if (inLoop) { result = factory.createContinue(t); } else { SemErr("continue used outside of loop"); } .) ";" | IfStatement<out result, inLoop> | ReturnStatement<out result> | Expression<out result> ";" ) . WhileStatement<out SLStatementNode result> = "while" (. Token whileToken = t; .) "(" Expression<out SLExpressionNode condition> ")" Block<out SLStatementNode body, true> (. result = factory.createWhile(whileToken, condition, body); .) . IfStatement<out SLStatementNode result, boolean inLoop> = "if" (. Token ifToken = t; .) "(" Expression<out SLExpressionNode condition> ")" Block<out SLStatementNode thenPart, inLoop> (. SLStatementNode elsePart = null; .) [ "else" Block<out elsePart, inLoop> ] (. result = factory.createIf(ifToken, condition, thenPart, elsePart); .) . ReturnStatement<out SLStatementNode result> = "return" (. Token returnToken = t; SLExpressionNode value = null; .) [ Expression<out value> ] (. result = factory.createReturn(returnToken, value); .) ";" . Expression<out SLExpressionNode result> = LogicTerm<out result> { "||" (. Token op = t; .) LogicTerm<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) } . LogicTerm<out SLExpressionNode result> = LogicFactor<out result> { "&&" (. Token op = t; .) LogicFactor<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) } . LogicFactor<out SLExpressionNode result> = Arithmetic<out result> [ ("<" | "<=" | ">" | ">=" | "==" | "!=" ) (. Token op = t; .) Arithmetic<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) ] . Arithmetic<out SLExpressionNode result> = Term<out result> { ("+" | "-") (. Token op = t; .) Term<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) } . Term<out SLExpressionNode result> = Factor<out result> { ("*" | "/") (. Token op = t; .) Factor<out SLExpressionNode right> (. result = factory.createBinary(op, result, right); .) } . Factor<out SLExpressionNode result> = (. result = null; .) ( identifier ( MemberExpression<out result, null, null, t> | (. result = factory.createRead(t); .) ) | stringLiteral (. result = factory.createStringLiteral(t); .) | numericLiteral (. result = factory.createNumericLiteral(t); .) | "(" (. int start = t.charPos; .) Expression<out result> (. SLExpressionNode expr = result; .) ")" (. int length = (t.charPos + t.val.length()) - start; .) (. result = factory.createParenExpression(expr, start, length); .) ) . MemberExpression<out SLExpressionNode result, SLExpressionNode r, SLExpressionNode assignmentReceiver, Token assignmentName> = (. result = null; SLExpressionNode receiver = r; Token nestedAssignmentName = null; .) ( "(" (. List<SLExpressionNode> parameters = new ArrayList<>(); SLExpressionNode parameter; if (receiver == null) { receiver = factory.createRead(assignmentName); } .) [ Expression<out parameter> (. parameters.add(parameter); .) { "," Expression<out parameter> (. parameters.add(parameter); .) } ] ")" (. Token finalToken = t; .) (. result = factory.createCall(receiver, parameters, finalToken); .) | "=" Expression<out SLExpressionNode value> (. if (assignmentName == null) { SemErr("invalid assignment target"); } else if (assignmentReceiver == null) { result = factory.createAssignment(assignmentName, value); } else { result = factory.createWriteProperty(assignmentReceiver, assignmentName, value); } .) | "." (. if (receiver == null) { receiver = factory.createRead(assignmentName); } .) identifier (. result = factory.createReadProperty(receiver, t); .) (. nestedAssignmentName = t; .) ) [ MemberExpression<out result, result, receiver, nestedAssignmentName> ] . END SimpleLanguage.