List of usage examples for org.antlr.v4.runtime.misc Interval startsAfter
public boolean startsAfter(Interval other)
From source file:info.fulloo.trygve.parser.Pass1Listener.java
License:Open Source License
@Override public void exitAbelian_atom(KantParser.Abelian_atomContext ctx) { // abelian_expr // | NEW message // | NEW type_name // | NEW type_name '[' expr ']' // | NEW JAVA_ID type_list '(' argument_list ')' // | abelian_atom '.' JAVA_ID // | abelian_atom '.' message // | builtin_type_name '.' message // | null_expr // | JAVA_ID // | abelian_atom ABELIAN_INCREMENT_OP // | ABELIAN_INCREMENT_OP abelian_atom // | constant // | '(' abelian_expr ')' // | abelian_atom '[' expr ']' // | abelian_atom '[' expr ']' ABELIAN_INCREMENT_OP // | ABELIAN_INCREMENT_OP abelian_atom '[' expr ']' // | ABELIAN_INCREMENT_OP abelian_atom '.' JAVA_ID // | abelian_atom '.' JAVA_ID ABELIAN_INCREMENT_OP // | /* this. */ message // | abelian_atom '.' CLONE // All passes. EXPLICITLY INVOKED FROM PASS 4 final int lineNumber = ctx.getStart().getLine(); Expression expression = null; if (null != ctx.NEW() && null != ctx.type_list() && null != ctx.argument_list()) { // | NEW JAVA_ID type_list '(' argument_list ')' Type type = null;/*from w ww . j a v a2 s.co m*/ final ActualArgumentList argument_list = parsingData_.popArgumentList(); final List<String> typeParameterNameList = parsingData_.popTypeNameList(); final String JAVA_ID = ctx.JAVA_ID().getText(); final TemplateDeclaration templateDeclaration = currentScope_ .lookupTemplateDeclarationRecursive(JAVA_ID); if (null == templateDeclaration) { errorHook5p2(ErrorIncidenceType.Fatal, lineNumber, "Cannot find template ", JAVA_ID, "", ""); type = new ErrorType(); expression = new ErrorExpression(null); } else { final StringBuffer typeNameBuffer = new StringBuffer(); typeNameBuffer.append(JAVA_ID); typeNameBuffer.append("<"); for (int i = 0; i < typeParameterNameList.size(); i++) { typeNameBuffer.append(typeParameterNameList.get(i)); if (i < typeParameterNameList.size() - 1) { typeNameBuffer.append(","); } } typeNameBuffer.append(">"); final String compoundTypeName = typeNameBuffer.toString(); type = currentScope_.lookupTypeDeclarationRecursive(compoundTypeName); if (null == type) { type = this.lookupOrCreateTemplateInstantiation(JAVA_ID, typeParameterNameList, lineNumber); if (null == type) { errorHook5p2(ErrorIncidenceType.Internal, lineNumber, "Cannot find template instantiation ", compoundTypeName, "", ""); type = new ErrorType(); } } final Type enclosingMegaType = Expression.nearestEnclosingMegaTypeOf(currentScope_); final Message message = new Message(compoundTypeName, argument_list, lineNumber, enclosingMegaType); final NewExpression newExpr = new NewExpression(type, message, lineNumber, enclosingMegaType); ctorCheck(type, message, lineNumber); addSelfAccordingToPass(type, message, currentScope_); expression = newExpr; } final MethodDeclaration constructor = type.enclosedScope().lookupMethodDeclaration(JAVA_ID, argument_list, false); if (null != constructor) { final boolean isAccessible = currentScope_.canAccessDeclarationWithAccessibility(constructor, constructor.accessQualifier(), lineNumber); if (isAccessible == false) { errorHook6p2(ErrorIncidenceType.Fatal, lineNumber, "Cannot access constructor `", constructor.signature().getText(), "' with `", constructor.accessQualifier().asString(), "' access qualifier.", ""); } // Create a new message just for checking (not dispatching) final String methodSelectorName = constructor.name(); final Type enclosingType = Expression.nearestEnclosingMegaTypeOf(currentScope_); final Message message = new Message(methodSelectorName, argument_list, lineNumber, enclosingType); final List<String> nonmatchingMethods = message.validInRunningEnviroment(constructor); if (0 < nonmatchingMethods.size()) { errorHook5p2(ErrorIncidenceType.Fatal, lineNumber, "The parameters to script `", methodSelectorName + message.argumentList().selflessGetText(), "' have scripts that are unavailable outside this Context, ", "though some formal parameters of " + methodSelectorName + " presume they are available (they are likely Role scripts):"); for (final String badMethod : nonmatchingMethods) { errorHook5p2(ErrorIncidenceType.Fatal, lineNumber, "\t", badMethod, "", ""); } } } expression = checkNakedNew(expression); if (printProductionsDebug) { System.err.println("abelian_atom : NEW JAVA_ID type_list '(' argument_list ')'"); } } else if (null == ctx.ABELIAN_INCREMENT_OP() && null != ctx.abelian_atom() && null != ctx.JAVA_ID()) { // | abelian_atom '.' JAVA_ID // The following line DOES pop the expression stack final ExpressionStackAPI rawExpression = this.exprFromExprDotJAVA_ID(ctx.JAVA_ID(), ctx.getStart(), null); assert rawExpression instanceof Expression; expression = (Expression) rawExpression; if (printProductionsDebug) { System.err.print("abelian_atom : abelian_atom '.' JAVA_ID ("); System.err.print(ctx.JAVA_ID().getText()); System.err.println(")"); } } else if (null == ctx.builtin_type_name() && null != ctx.abelian_atom() && null != ctx.message()) { // | abelian_atom '.' message // This routine actually does pop the expressions stack (and the Message stack) expression = this.messageSend(ctx.getStart(), ctx.abelian_atom(), null); if (null == expression) { errorHook5p2(ErrorIncidenceType.Fatal, lineNumber, "No match for call: ", ctx.abelian_atom().getText(), ".", ctx.message().getText()); expression = new ErrorExpression(null); } if (printProductionsDebug) { System.err.println("abelian_product : abelian_atom '.' message"); } } else if (null == ctx.NEW() && null != ctx.builtin_type_name() && null != ctx.message()) { // | builtin_type_name '.' message // (e.g., String.join) // This routine actually does pop the expressions stack (and the Message stack) expression = this.messageSend(ctx.getStart(), ctx.abelian_atom(), ctx.builtin_type_name()); if (null == expression || expression.isError()) { errorHook5p2(ErrorIncidenceType.Fatal, lineNumber, "No match for call: ", ctx.type_name().getText(), ".", ctx.message().getText()); expression = new ErrorExpression(null); } if (printProductionsDebug) { System.err.println("type_name : abelian_atom '.' message"); } } else if (null != ctx.NEW()) { // NEW message // NEW type_name '[' expr ']' final KantParser.ExprContext sizeExprCtx = (null == ctx.expr()) ? null : ((ctx.expr().size() == 0) ? null : ctx.expr(0)); final List<ParseTree> ctxChildren = ctx.children; final Token ctxGetStart = ctx.getStart(); final MessageContext ctxMessage = ctx.message(); expression = this.newExpr(ctxChildren, ctxGetStart, sizeExprCtx, ctxMessage); if (expression instanceof NullExpression == false && expression.isntError()) { expression = checkNakedNew(expression); } if (printProductionsDebug) { System.err.print("expr : "); if (expression instanceof NewExpression) { System.err.print(((NewExpression) expression).getText()); } else { System.err.print("<unknown class>"); } System.err.println(""); } } else if (null != ctx.null_expr()) { // | null_expr expression = parsingData_.popExpression(); if (printProductionsDebug) { System.err.print("abelian_atom : null_expr"); } } else if (null != ctx.JAVA_ID() && null == ctx.ABELIAN_INCREMENT_OP() && (null == ctx.abelian_expr())) { // | JAVA_ID expression = idExpr(ctx.JAVA_ID(), ctx.getStart()); if (printProductionsDebug) { System.err.print("abelian_atom : JAVA_ID ("); System.err.print(ctx.JAVA_ID().getText()); System.err.println(")"); } } else if (null != ctx.abelian_atom() && null != ctx.ABELIAN_INCREMENT_OP() && (null == ctx.abelian_expr())) { // | abelian_atom ABELIAN_INCREMENT_OP // | ABELIAN_INCREMENT_OP abelian_atom final Interval AbelianAtomInterval = ctx.abelian_atom().getSourceInterval(); final Interval OperatorInterval = ctx.ABELIAN_INCREMENT_OP().getSourceInterval(); final PreOrPost preOrPost = AbelianAtomInterval.startsAfter(OperatorInterval) ? UnaryopExpressionWithSideEffect.PreOrPost.Pre : UnaryopExpressionWithSideEffect.PreOrPost.Post; if (parsingData_.currentExpressionExists()) { expression = parsingData_.popExpression(); assert null != expression; expression = new UnaryopExpressionWithSideEffect(expression, ctx.ABELIAN_INCREMENT_OP().getText(), preOrPost); assert null != expression; } else { expression = new ErrorExpression(null); } if (printProductionsDebug) { switch (preOrPost) { case Post: System.err.print("abelian_atom : abelian_atom ("); System.err.print(ctx.abelian_atom().getText()); System.err.println(") ABELIAN_INCREMENT_OP"); break; case Pre: System.err.print("abelian_atom : ABELIAN_INCREMENT_OP abelian_atom ("); System.err.print(ctx.abelian_atom().getText()); System.err.println(")"); break; } } checkForIncrementOpViolatingIdentifierConstness((UnaryopExpressionWithSideEffect) expression, ctx.getStart()); } else if (null != ctx.ABELIAN_INCREMENT_OP() && null != ctx.abelian_atom() && null != ctx.JAVA_ID()) { // | ABELIAN_INCREMENT_OP abelian_atom '.' JAVA_ID final ExpressionStackAPI rawExpression = this.exprFromExprDotJAVA_ID(ctx.JAVA_ID(), ctx.getStart(), ctx.ABELIAN_INCREMENT_OP()); assert rawExpression instanceof Expression; expression = (Expression) rawExpression; if (printProductionsDebug) { System.err.print("abelian_atom : '++' expr '.' JAVA_ID ("); System.err.print(ctx.JAVA_ID().getText()); System.err.println(")"); } } else if (null != ctx.constant()) { // | constant // expression = Expression.makeConstantExpressionFrom(ctx.constant().getText()); // is on the stack. We now have a "constant : ..." production, // so we don't make it here expression = parsingData_.popExpression(); if (printProductionsDebug) { System.err.println("abelian_atom : constant"); } } else if (null != ctx.abelian_expr() && null == ctx.JAVA_ID() && null == ctx.CLONE() && null == ctx.message() && (null == ctx.expr() || ctx.expr().size() == 0) && null == ctx.ABELIAN_INCREMENT_OP()) { // | '(' abelian_expr ')' expression = parsingData_.popExpression(); if (printProductionsDebug) { System.err.println("abelian_atom : '(' abelian_expr ')'"); } } else if (null != ctx.abelian_atom() && null == ctx.JAVA_ID() && null == ctx.CLONE() && null == ctx.message() && null != ctx.expr() && (ctx.expr().size() == 1) && null == ctx.ABELIAN_INCREMENT_OP()) { // | abelian_atom '[' expr ']' final Expression indexExpr = parsingData_.currentExpressionExists() ? parsingData_.popExpression() : new ErrorExpression(null); indexExpr.setResultIsConsumed(true); final Expression rawArrayBase = parsingData_.currentExpressionExists() ? parsingData_.popExpression() : new ErrorExpression(indexExpr); // The fidelity of this varies according to how much // type information we have at hand expression = processIndexExpression(rawArrayBase, indexExpr, lineNumber); if (printProductionsDebug) { System.err.println("abelian_atom : abelian_expr '[' expr ']'"); } } else if (null != ctx.abelian_atom() && null == ctx.JAVA_ID() && null == ctx.CLONE() && null == ctx.message() && null != ctx.expr() && (ctx.expr().size() > 0) && null != ctx.ABELIAN_INCREMENT_OP()) { // | abelian_atom '[' expr ']' ABELIAN_INCREMENT_OP // | ABELIAN_INCREMENT_OP abelian_atom '[' expr ']' ParseTree arrayBase; KantParser.ExprContext theIndex; if (ctx.expr().size() == 2) { arrayBase = ctx.expr(0); theIndex = ctx.expr(1); } else { arrayBase = ctx.abelian_atom(); theIndex = ctx.expr(0); } final ExpressionStackAPI rawExpression = processIndexedArrayElement(arrayBase, theIndex, ctx.ABELIAN_INCREMENT_OP()); assert rawExpression instanceof Expression; expression = (Expression) rawExpression; checkForIncrementOpViolatingConstness((ArrayIndexExpressionUnaryOp) expression, ctx.getStart()); if (printProductionsDebug) { if (null != ctx.abelian_expr()) { System.err.println("abelian_atom : abelian_expr '[' expr ']' ABELIAN_INCREMENT_OP"); } else { System.err.println("abelian_atom : ABELIAN_INCREMENT_OP expr '[' expr ']'"); } } } else if (null != ctx.abelian_atom() && null != ctx.JAVA_ID() && null == ctx.CLONE() && null == ctx.message() && (null == ctx.expr() || (ctx.expr().size() == 0)) && null != ctx.ABELIAN_INCREMENT_OP()) { // | abelian_atom '.' JAVA_ID ABELIAN_INCREMENT_OP final ExpressionStackAPI rawExpression = this.exprFromExprDotJAVA_ID(ctx.JAVA_ID(), ctx.getStart(), ctx.ABELIAN_INCREMENT_OP()); assert rawExpression instanceof Expression; expression = (Expression) rawExpression; if (printProductionsDebug) { System.err.print("abelian_atom : abelian_atom '.' JAVA_ID ("); System.err.print(ctx.JAVA_ID().getText()); System.err.println(") ++"); } } else if ((null == ctx.abelian_atom()) && (null == ctx.abelian_expr()) && null != ctx.message()) { // | /* this. */ message // This routine actually does pop the expressions stack (and the Message stack) expression = this.messageSend(ctx.getStart(), null, null); if (printProductionsDebug) { System.err.println("abelian_atom : /* this. */ message"); } } else if (null != ctx.abelian_atom() && null != ctx.CLONE()) { // | abelian_atom '.' CLONE final Expression qualifier = parsingData_.popExpression(); expression = new DupMessageExpression(qualifier, qualifier.type()); if (qualifier.isError() || qualifier.type().isError()) { expression = new ErrorExpression(expression); } if (printProductionsDebug) { System.err.println("abelian_atom : abelian_expr '.' 'clone'"); } } else { assert false; } if (null != expression) { // null check is error stumbling check parsingData_.pushExpression(expression); } if (stackSnapshotDebug) stackSnapshotDebug(); }
From source file:info.fulloo.trygve.parser.Pass1Listener.java
License:Open Source License
private Expression processIndexedArrayElement(final ParseTree arrayExprCtx, final KantParser.ExprContext sexpCtx, final TerminalNode ABELIAN_INCREMENT_OPCtx) { // | abelian_expr '[' expr ']' ABELIAN_INCREMENT_OP // | ABELIAN_INCREMENT_OP expr '[' expr ']' Expression retval = null;// w ww . j a v a 2 s . com final Expression indexExpr = parsingData_.popExpression(); indexExpr.setResultIsConsumed(true); final Expression rawArrayBase = parsingData_.popExpression(); final Type rawArrayBaseType = rawArrayBase.type(); assert rawArrayBaseType instanceof ArrayType; final ArrayType arrayType = (ArrayType) rawArrayBaseType; final Type baseType = arrayType.baseType(); final ArrayExpression arrayBase = new ArrayExpression(rawArrayBase, baseType); arrayBase.setResultIsConsumed(true); final Interval JavaIDInterval = arrayExprCtx.getSourceInterval(); final Interval OperatorInterval = ABELIAN_INCREMENT_OPCtx.getSourceInterval(); final UnaryopExpressionWithSideEffect.PreOrPost preOrPost = JavaIDInterval.startsAfter(OperatorInterval) ? UnaryopExpressionWithSideEffect.PreOrPost.Pre : UnaryopExpressionWithSideEffect.PreOrPost.Post; retval = new ArrayIndexExpressionUnaryOp(arrayBase, indexExpr, ABELIAN_INCREMENT_OPCtx.getText(), preOrPost, sexpCtx.getStart().getLine()); return retval; }
From source file:info.fulloo.trygve.parser.Pass1Listener.java
License:Open Source License
private ExpressionStackAPI exprFromExprDotJAVA_ID(final TerminalNode ctxJAVA_ID, final Token ctxGetStart, final TerminalNode ctxABELIAN_INCREMENT_OP) { // Certified Pass 1 version ;-) There is no longer any Pass 2+ version UnaryopExpressionWithSideEffect.PreOrPost preOrPost; Type type = null;//from w w w.j ava 2s .com final ExpressionStackAPI qualifier = parsingData_.popRawExpression(); Expression expression = null; final String javaIdString = ctxJAVA_ID.getText(); preOrPost = UnaryopExpressionWithSideEffect.PreOrPost.Post; if (null != ctxABELIAN_INCREMENT_OP) { final Interval JavaIDInterval = ctxJAVA_ID.getSourceInterval(); final Interval OperatorInterval = ctxABELIAN_INCREMENT_OP.getSourceInterval(); preOrPost = JavaIDInterval.startsAfter(OperatorInterval) ? UnaryopExpressionWithSideEffect.PreOrPost.Pre : UnaryopExpressionWithSideEffect.PreOrPost.Post; } assert null != qualifier; if (null != qualifier.type() && qualifier.type().name().equals("Class")) { // This is where we handle types like "System" for System.out.print* // Now we need to get the actual class of that name final Type rawClass = currentScope_.lookupTypeDeclarationRecursive(qualifier.name()); assert rawClass instanceof ClassType; final ClassType theClass = (ClassType) rawClass; final ObjectDeclaration odecl = theClass.type().enclosedScope().lookupObjectDeclaration(javaIdString); if (null != odecl) { // It must be static final ObjectDeclaration odecl2 = theClass.type().enclosedScope() .lookupStaticDeclaration(javaIdString); if (null == odecl2) { errorHook5p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Attempt to access instance member `", javaIdString, "' as a member of class `" + theClass.name(), "'."); type = new ErrorType(); } else { type = odecl.type(); } assert type != null; if (null != ctxABELIAN_INCREMENT_OP) { expression = new QualifiedClassMemberExpressionUnaryOp(theClass, javaIdString, type, ctxABELIAN_INCREMENT_OP.getText(), preOrPost); } else { expression = new QualifiedClassMemberExpression(theClass, javaIdString, type); } } else { errorHook5p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Member `", javaIdString, "' of `" + qualifier.name(), "' is not defined."); expression = new ErrorExpression(null); } } else { final Expression object = (Expression) qualifier; object.setResultIsConsumed(true); final Type objectType = object.type(); Declaration odecl = null; if (objectType.isntError()) { odecl = objectType.enclosedScope().lookupObjectDeclarationRecursive(javaIdString); } if (null != odecl) { type = odecl.type(); assert type != null; if (null != ctxABELIAN_INCREMENT_OP) { expression = new QualifiedIdentifierExpressionUnaryOp(object, javaIdString, type, ctxABELIAN_INCREMENT_OP.getText(), preOrPost); } else { expression = new QualifiedIdentifierExpression(object, javaIdString, type); } if (odecl instanceof ObjectDeclaration) { final ObjectDeclaration odeclAsOdecl = (ObjectDeclaration) odecl; final boolean isAccessible = currentScope_.canAccessDeclarationWithAccessibility(odeclAsOdecl, odeclAsOdecl.accessQualifier_, ctxGetStart.getLine()); if (isAccessible == false) { errorHook6p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Cannot access expression `", expression.getText(), "' with `", odeclAsOdecl.accessQualifier_.asString(), "' access qualifier.", ""); } } else if (odecl.isntError()) { errorHook5p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Cannot access expression `", expression.getText(), "' with non-object type declaration", ""); } } else if (null == (expression = degenerateProcedureCheck(qualifier, objectType, javaIdString, ctxGetStart.getLine()))) { if (object.isntError() && object.type().isntError()) { errorHook5p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Field `", javaIdString, "' not found as member of `", object.type().name() + "'."); } type = new ErrorType(); odecl = new ErrorDeclaration(""); expression = new ErrorExpression(null); } } return null == expression ? new ErrorExpression(null) : expression; }