0001: // ===========================================================================
0002: // This file has been generated by
0003: // Rats! Parser Generator, version 1.13.1,
0004: // (C) 2004-2007 Robert Grimm,
0005: // on Monday, October 8, 2007 at 5:40:15 PM.
0006: // Edit at your own risk.
0007: // ===========================================================================
0008:
0009: package xtc.lang;
0010:
0011: import java.io.Reader;
0012: import java.io.BufferedReader;
0013: import java.io.BufferedWriter;
0014: import java.io.File;
0015: import java.io.FileReader;
0016: import java.io.OutputStreamWriter;
0017: import java.io.IOException;
0018:
0019: import xtc.util.Action;
0020: import xtc.util.Pair;
0021:
0022: import xtc.tree.Node;
0023: import xtc.tree.GNode;
0024: import xtc.tree.Printer;
0025:
0026: import xtc.parser.ParserBase;
0027: import xtc.parser.Column;
0028: import xtc.parser.Result;
0029: import xtc.parser.SemanticValue;
0030: import xtc.parser.ParseError;
0031:
0032: /**
0033: * Packrat parser for grammar <code>xtc.lang.TypedLambda</code>.
0034: *
0035: * <p />This class has been generated by the <i>Rats!</i> parser
0036: * generator, version 1.13.1, (C) 2004-2007 Robert Grimm.
0037: */
0038: public final class TypedLambdaParser extends ParserBase {
0039:
0040: // =========================================================================
0041:
0042: /** Memoization table column. */
0043: static final class TypedLambdaParserColumn extends Column {
0044: Result fApplication;
0045: Result fBasicExpression;
0046: Result fIdentifier;
0047: Result fFunctionType;
0048: Result fBasicType;
0049: Result fOPEN;
0050: Result fCLOSE;
0051: }
0052:
0053: // =========================================================================
0054:
0055: /**
0056: * Create a new packrat parser.
0057: *
0058: * @param reader The reader.
0059: * @param file The file name.
0060: */
0061: public TypedLambdaParser(final Reader reader, final String file) {
0062: super (reader, file);
0063: }
0064:
0065: /**
0066: * Create a new packrat parser.
0067: *
0068: * @param reader The file reader.
0069: * @param file The file name.
0070: * @param size The file size.
0071: */
0072: public TypedLambdaParser(final Reader reader, final String file,
0073: final int size) {
0074: super (reader, file, size);
0075: }
0076:
0077: // =========================================================================
0078:
0079: protected Column newColumn() {
0080: return new TypedLambdaParserColumn();
0081: }
0082:
0083: // =========================================================================
0084:
0085: /**
0086: * Parse nonterminal xtc.lang.TypedLambda.Expression.
0087: *
0088: * @param yyStart The index.
0089: * @return The result.
0090: * @throws IOException Signals an I/O error.
0091: */
0092: public Result pExpression(final int yyStart) throws IOException {
0093: Result yyResult;
0094: Node yyValue;
0095: ParseError yyError = ParseError.DUMMY;
0096:
0097: // Alternative 1.
0098:
0099: yyResult = pApplication(yyStart);
0100: yyError = yyResult.select(yyError);
0101: if (yyResult.hasValue()) {
0102: yyValue = yyResult.semanticValue();
0103:
0104: yyResult = pEndOfFile(yyResult.index);
0105: yyError = yyResult.select(yyError);
0106: if (yyResult.hasValue()) {
0107:
0108: return yyResult.createValue(yyValue, yyError);
0109: }
0110: }
0111:
0112: // Done.
0113: return yyError;
0114: }
0115:
0116: // =========================================================================
0117:
0118: /**
0119: * Parse nonterminal xtc.lang.TypedLambda.Application.
0120: *
0121: * @param yyStart The index.
0122: * @return The result.
0123: * @throws IOException Signals an I/O error.
0124: */
0125: private Result pApplication(final int yyStart) throws IOException {
0126: TypedLambdaParserColumn yyColumn = (TypedLambdaParserColumn) column(yyStart);
0127: if (null == yyColumn.fApplication)
0128: yyColumn.fApplication = pApplication$1(yyStart);
0129: return yyColumn.fApplication;
0130: }
0131:
0132: /** Actually parse xtc.lang.TypedLambda.Application. */
0133: private Result pApplication$1(final int yyStart) throws IOException {
0134: Result yyResult;
0135: int yyRepetition1;
0136: Pair<Action<Node>> yyRepValue1;
0137: Node yyValue;
0138: ParseError yyError = ParseError.DUMMY;
0139:
0140: // Alternative <Base>.
0141:
0142: yyResult = pBasicExpression(yyStart);
0143: yyError = yyResult.select(yyError);
0144: if (yyResult.hasValue()) {
0145: final Node v$g$3 = yyResult.semanticValue();
0146:
0147: yyRepetition1 = yyResult.index;
0148: yyRepValue1 = Pair.empty();
0149: while (true) {
0150:
0151: yyResult = pApplication$$Tail1(yyRepetition1);
0152: yyError = yyResult.select(yyError);
0153: if (yyResult.hasValue()) {
0154: final Action<Node> v$4 = yyResult.semanticValue();
0155:
0156: yyRepetition1 = yyResult.index;
0157: yyRepValue1 = new Pair<Action<Node>>(v$4,
0158: yyRepValue1);
0159: continue;
0160: }
0161: break;
0162: }
0163: { // Start scope for v$5.
0164: final Pair<Action<Node>> v$5 = yyRepValue1.reverse();
0165:
0166: yyValue = apply(v$5, v$g$3, yyStart);
0167:
0168: return new SemanticValue(yyValue, yyRepetition1,
0169: yyError);
0170: } // End scope for v$5.
0171: }
0172:
0173: // Done.
0174: return yyError;
0175: }
0176:
0177: // =========================================================================
0178:
0179: /**
0180: * Parse synthetic nonterminal xtc.lang.TypedLambda.Application$$Tail1.
0181: *
0182: * @param yyStart The index.
0183: * @return The result.
0184: * @throws IOException Signals an I/O error.
0185: */
0186: private Result pApplication$$Tail1(final int yyStart)
0187: throws IOException {
0188: Result yyResult;
0189: Action<Node> yyValue;
0190: ParseError yyError = ParseError.DUMMY;
0191:
0192: // Alternative <Recursion>.
0193:
0194: yyResult = pBasicExpression(yyStart);
0195: yyError = yyResult.select(yyError);
0196: if (yyResult.hasValue()) {
0197: final Node v$g$2 = yyResult.semanticValue();
0198:
0199: yyValue = new Action<Node>() {
0200: public Node run(Node v$1) {
0201: return GNode.create("Application", v$1, v$g$2);
0202: }
0203: };
0204:
0205: return yyResult.createValue(yyValue, yyError);
0206: }
0207:
0208: // Done.
0209: return yyError;
0210: }
0211:
0212: // =========================================================================
0213:
0214: /**
0215: * Parse nonterminal xtc.lang.TypedLambda.BasicExpression.
0216: *
0217: * @param yyStart The index.
0218: * @return The result.
0219: * @throws IOException Signals an I/O error.
0220: */
0221: private Result pBasicExpression(final int yyStart)
0222: throws IOException {
0223: TypedLambdaParserColumn yyColumn = (TypedLambdaParserColumn) column(yyStart);
0224: if (null == yyColumn.fBasicExpression)
0225: yyColumn.fBasicExpression = pBasicExpression$1(yyStart);
0226: return yyColumn.fBasicExpression;
0227: }
0228:
0229: /** Actually parse xtc.lang.TypedLambda.BasicExpression. */
0230: private Result pBasicExpression$1(final int yyStart)
0231: throws IOException {
0232: Result yyResult;
0233: Node yyValue;
0234: ParseError yyError = ParseError.DUMMY;
0235:
0236: // Alternative <Abstraction>.
0237:
0238: yyResult = pAbstraction(yyStart);
0239: yyError = yyResult.select(yyError);
0240: if (yyResult.hasValue()) {
0241: yyValue = yyResult.semanticValue();
0242:
0243: return yyResult.createValue(yyValue, yyError);
0244: }
0245:
0246: // Alternative <Identifier>.
0247:
0248: yyResult = pIdentifier(yyStart);
0249: yyError = yyResult.select(yyError);
0250: if (yyResult.hasValue()) {
0251: yyValue = yyResult.semanticValue();
0252:
0253: return yyResult.createValue(yyValue, yyError);
0254: }
0255:
0256: // Alternative <Integer>.
0257:
0258: yyResult = pIntegerConstant(yyStart);
0259: yyError = yyResult.select(yyError);
0260: if (yyResult.hasValue()) {
0261: yyValue = yyResult.semanticValue();
0262:
0263: return yyResult.createValue(yyValue, yyError);
0264: }
0265:
0266: // Alternative <String>.
0267:
0268: yyResult = pStringConstant(yyStart);
0269: yyError = yyResult.select(yyError);
0270: if (yyResult.hasValue()) {
0271: yyValue = yyResult.semanticValue();
0272:
0273: return yyResult.createValue(yyValue, yyError);
0274: }
0275:
0276: // Alternative <Parenthesized>.
0277:
0278: yyResult = pOPEN(yyStart);
0279: yyError = yyResult.select(yyError);
0280: if (yyResult.hasValue()) {
0281:
0282: yyResult = pApplication(yyResult.index);
0283: yyError = yyResult.select(yyError);
0284: if (yyResult.hasValue()) {
0285: yyValue = yyResult.semanticValue();
0286:
0287: yyResult = pCLOSE(yyResult.index);
0288: yyError = yyResult.select(yyError);
0289: if (yyResult.hasValue()) {
0290:
0291: return yyResult.createValue(yyValue, yyError);
0292: }
0293: }
0294: }
0295:
0296: // Done.
0297: return yyError;
0298: }
0299:
0300: // =========================================================================
0301:
0302: /**
0303: * Parse nonterminal xtc.lang.TypedLambda.Abstraction.
0304: *
0305: * @param yyStart The index.
0306: * @return The result.
0307: * @throws IOException Signals an I/O error.
0308: */
0309: private Result pAbstraction(final int yyStart) throws IOException {
0310: Result yyResult;
0311: Node yyValue;
0312: ParseError yyError = ParseError.DUMMY;
0313:
0314: // Alternative 1.
0315:
0316: yyResult = pLAMBDA(yyStart);
0317: yyError = yyResult.select(yyError);
0318: if (yyResult.hasValue()) {
0319:
0320: yyResult = pIdentifier(yyResult.index);
0321: yyError = yyResult.select(yyError);
0322: if (yyResult.hasValue()) {
0323: final Node v$g$1 = yyResult.semanticValue();
0324:
0325: yyResult = pCOLON(yyResult.index);
0326: yyError = yyResult.select(yyError);
0327: if (yyResult.hasValue()) {
0328:
0329: yyResult = pFunctionType(yyResult.index);
0330: yyError = yyResult.select(yyError);
0331: if (yyResult.hasValue()) {
0332: final Node v$g$2 = yyResult.semanticValue();
0333:
0334: yyResult = pDOT(yyResult.index);
0335: yyError = yyResult.select(yyError);
0336: if (yyResult.hasValue()) {
0337:
0338: yyResult = pApplication(yyResult.index);
0339: yyError = yyResult.select(yyError);
0340: if (yyResult.hasValue()) {
0341: final Node v$g$3 = yyResult
0342: .semanticValue();
0343:
0344: yyValue = GNode.create("Abstraction",
0345: v$g$1, v$g$2, v$g$3);
0346: yyValue.setLocation(location(yyStart));
0347:
0348: return yyResult.createValue(yyValue,
0349: yyError);
0350: }
0351: }
0352: }
0353: }
0354: }
0355: }
0356:
0357: // Done.
0358: return yyError;
0359: }
0360:
0361: // =========================================================================
0362:
0363: /**
0364: * Parse nonterminal xtc.lang.TypedLambda.Identifier.
0365: *
0366: * @param yyStart The index.
0367: * @return The result.
0368: * @throws IOException Signals an I/O error.
0369: */
0370: private Result pIdentifier(final int yyStart) throws IOException {
0371: TypedLambdaParserColumn yyColumn = (TypedLambdaParserColumn) column(yyStart);
0372: if (null == yyColumn.fIdentifier)
0373: yyColumn.fIdentifier = pIdentifier$1(yyStart);
0374: return yyColumn.fIdentifier;
0375: }
0376:
0377: /** Actually parse xtc.lang.TypedLambda.Identifier. */
0378: private Result pIdentifier$1(final int yyStart) throws IOException {
0379: Result yyResult;
0380: Node yyValue;
0381: ParseError yyError = ParseError.DUMMY;
0382:
0383: // Alternative 1.
0384:
0385: yyResult = pIdentifierCharacters(yyStart);
0386: yyError = yyResult.select(yyError);
0387: if (yyResult.hasValue()) {
0388: final String v$g$1 = yyResult.semanticValue();
0389:
0390: yyResult = pSpacing(yyResult.index);
0391: yyError = yyResult.select(yyError);
0392: if (yyResult.hasValue()) {
0393:
0394: yyValue = GNode.create("Identifier", v$g$1);
0395: yyValue.setLocation(location(yyStart));
0396:
0397: return yyResult.createValue(yyValue, yyError);
0398: }
0399: }
0400:
0401: // Done.
0402: return yyError;
0403: }
0404:
0405: // =========================================================================
0406:
0407: /**
0408: * Parse nonterminal xtc.lang.TypedLambda.IdentifierCharacters.
0409: *
0410: * @param yyStart The index.
0411: * @return The result.
0412: * @throws IOException Signals an I/O error.
0413: */
0414: private Result pIdentifierCharacters(final int yyStart)
0415: throws IOException {
0416: int yyC;
0417: int yyIndex;
0418: int yyRepetition1;
0419: boolean yyRepeated1;
0420: String yyValue;
0421: ParseError yyError = ParseError.DUMMY;
0422:
0423: // Alternative 1.
0424:
0425: yyRepetition1 = yyStart;
0426: yyRepeated1 = false;
0427: while (true) {
0428:
0429: yyC = character(yyRepetition1);
0430: if (-1 != yyC) {
0431: yyIndex = yyRepetition1 + 1;
0432: if ((('A' <= yyC) && (yyC <= 'Z'))
0433: || (('a' <= yyC) && (yyC <= 'z'))) {
0434:
0435: yyRepetition1 = yyIndex;
0436: yyRepeated1 = true;
0437: continue;
0438: }
0439: }
0440: break;
0441: }
0442:
0443: if (yyRepeated1) {
0444:
0445: yyValue = difference(yyStart, yyRepetition1);
0446:
0447: return new SemanticValue(yyValue, yyRepetition1, yyError);
0448: }
0449:
0450: // Done.
0451: yyError = yyError.select("identifier characters expected",
0452: yyStart);
0453: return yyError;
0454: }
0455:
0456: // =========================================================================
0457:
0458: /**
0459: * Parse nonterminal xtc.lang.TypedLambda.IntegerConstant.
0460: *
0461: * @param yyStart The index.
0462: * @return The result.
0463: * @throws IOException Signals an I/O error.
0464: */
0465: private Result pIntegerConstant(final int yyStart)
0466: throws IOException {
0467: Result yyResult;
0468: Node yyValue;
0469: ParseError yyError = ParseError.DUMMY;
0470:
0471: // Alternative 1.
0472:
0473: yyResult = pIntegerCharacters(yyStart);
0474: yyError = yyResult.select(yyError);
0475: if (yyResult.hasValue()) {
0476: final String v$g$1 = yyResult.semanticValue();
0477:
0478: yyResult = pSpacing(yyResult.index);
0479: yyError = yyResult.select(yyError);
0480: if (yyResult.hasValue()) {
0481:
0482: yyValue = GNode.create("IntegerConstant", v$g$1);
0483: yyValue.setLocation(location(yyStart));
0484:
0485: return yyResult.createValue(yyValue, yyError);
0486: }
0487: }
0488:
0489: // Done.
0490: return yyError;
0491: }
0492:
0493: // =========================================================================
0494:
0495: /**
0496: * Parse nonterminal xtc.lang.TypedLambda.IntegerCharacters.
0497: *
0498: * @param yyStart The index.
0499: * @return The result.
0500: * @throws IOException Signals an I/O error.
0501: */
0502: private Result pIntegerCharacters(final int yyStart)
0503: throws IOException {
0504: int yyC;
0505: int yyIndex;
0506: int yyRepetition1;
0507: String yyValue;
0508: ParseError yyError = ParseError.DUMMY;
0509:
0510: // Alternative 1.
0511:
0512: yyC = character(yyStart);
0513: if (-1 != yyC) {
0514: yyIndex = yyStart + 1;
0515:
0516: switch (yyC) {
0517: case '1':
0518: case '2':
0519: case '3':
0520: case '4':
0521: case '5':
0522: case '6':
0523: case '7':
0524: case '8':
0525: case '9': {
0526: yyRepetition1 = yyIndex;
0527: while (true) {
0528:
0529: yyC = character(yyRepetition1);
0530: if (-1 != yyC) {
0531: yyIndex = yyRepetition1 + 1;
0532:
0533: switch (yyC) {
0534: case '0':
0535: case '1':
0536: case '2':
0537: case '3':
0538: case '4':
0539: case '5':
0540: case '6':
0541: case '7':
0542: case '8':
0543: case '9': {
0544: yyRepetition1 = yyIndex;
0545: continue;
0546: }
0547:
0548: default:
0549: /* No match. */
0550: }
0551: }
0552: break;
0553: }
0554:
0555: yyValue = difference(yyStart, yyRepetition1);
0556:
0557: return new SemanticValue(yyValue, yyRepetition1,
0558: yyError);
0559: }
0560:
0561: default:
0562: /* No match. */
0563: }
0564: }
0565:
0566: // Done.
0567: yyError = yyError
0568: .select("integer characters expected", yyStart);
0569: return yyError;
0570: }
0571:
0572: // =========================================================================
0573:
0574: /**
0575: * Parse nonterminal xtc.lang.TypedLambda.StringConstant.
0576: *
0577: * @param yyStart The index.
0578: * @return The result.
0579: * @throws IOException Signals an I/O error.
0580: */
0581: private Result pStringConstant(final int yyStart)
0582: throws IOException {
0583: Result yyResult;
0584: Node yyValue;
0585: ParseError yyError = ParseError.DUMMY;
0586:
0587: // Alternative 1.
0588:
0589: yyResult = pStringCharacters(yyStart);
0590: yyError = yyResult.select(yyError);
0591: if (yyResult.hasValue()) {
0592: final String v$g$1 = yyResult.semanticValue();
0593:
0594: yyResult = pSpacing(yyResult.index);
0595: yyError = yyResult.select(yyError);
0596: if (yyResult.hasValue()) {
0597:
0598: yyValue = GNode.create("StringConstant", v$g$1);
0599: yyValue.setLocation(location(yyStart));
0600:
0601: return yyResult.createValue(yyValue, yyError);
0602: }
0603: }
0604:
0605: // Done.
0606: return yyError;
0607: }
0608:
0609: // =========================================================================
0610:
0611: /**
0612: * Parse nonterminal xtc.lang.TypedLambda.StringCharacters.
0613: *
0614: * @param yyStart The index.
0615: * @return The result.
0616: * @throws IOException Signals an I/O error.
0617: */
0618: private Result pStringCharacters(final int yyStart)
0619: throws IOException {
0620: int yyC;
0621: int yyIndex;
0622: int yyRepetition1;
0623: String yyValue;
0624: ParseError yyError = ParseError.DUMMY;
0625:
0626: // Alternative 1.
0627:
0628: yyC = character(yyStart);
0629: if ('\"' == yyC) {
0630: yyIndex = yyStart + 1;
0631:
0632: yyRepetition1 = yyIndex;
0633: while (true) {
0634:
0635: yyC = character(yyRepetition1);
0636: if (-1 != yyC) {
0637: yyIndex = yyRepetition1 + 1;
0638: if ('\"' != yyC) {
0639:
0640: yyRepetition1 = yyIndex;
0641: continue;
0642: }
0643: }
0644: break;
0645: }
0646:
0647: yyC = character(yyRepetition1);
0648: if ('\"' == yyC) {
0649: yyIndex = yyRepetition1 + 1;
0650:
0651: yyValue = difference(yyStart, yyIndex);
0652:
0653: return new SemanticValue(yyValue, yyIndex, yyError);
0654: }
0655: }
0656:
0657: // Done.
0658: yyError = yyError.select("string characters expected", yyStart);
0659: return yyError;
0660: }
0661:
0662: // =========================================================================
0663:
0664: /**
0665: * Parse nonterminal xtc.lang.TypedLambda.FunctionType.
0666: *
0667: * @param yyStart The index.
0668: * @return The result.
0669: * @throws IOException Signals an I/O error.
0670: */
0671: private Result pFunctionType(final int yyStart) throws IOException {
0672: TypedLambdaParserColumn yyColumn = (TypedLambdaParserColumn) column(yyStart);
0673: if (null == yyColumn.fFunctionType)
0674: yyColumn.fFunctionType = pFunctionType$1(yyStart);
0675: return yyColumn.fFunctionType;
0676: }
0677:
0678: /** Actually parse xtc.lang.TypedLambda.FunctionType. */
0679: private Result pFunctionType$1(final int yyStart)
0680: throws IOException {
0681: Result yyResult;
0682: Node yyValue;
0683: ParseError yyError = ParseError.DUMMY;
0684:
0685: // Alternative <Recursion>.
0686:
0687: yyResult = pBasicType(yyStart);
0688: yyError = yyResult.select(yyError);
0689: if (yyResult.hasValue()) {
0690: final Node v$g$1 = yyResult.semanticValue();
0691:
0692: yyResult = pARROW(yyResult.index);
0693: yyError = yyResult.select(yyError);
0694: if (yyResult.hasValue()) {
0695:
0696: yyResult = pFunctionType(yyResult.index);
0697: yyError = yyResult.select(yyError);
0698: if (yyResult.hasValue()) {
0699: final Node v$g$2 = yyResult.semanticValue();
0700:
0701: yyValue = GNode
0702: .create("FunctionType", v$g$1, v$g$2);
0703: yyValue.setLocation(location(yyStart));
0704:
0705: return yyResult.createValue(yyValue, yyError);
0706: }
0707: }
0708: }
0709:
0710: // Alternative <Base>.
0711:
0712: yyResult = pBasicType(yyStart);
0713: yyError = yyResult.select(yyError);
0714: if (yyResult.hasValue()) {
0715: yyValue = yyResult.semanticValue();
0716:
0717: return yyResult.createValue(yyValue, yyError);
0718: }
0719:
0720: // Done.
0721: return yyError;
0722: }
0723:
0724: // =========================================================================
0725:
0726: /**
0727: * Parse nonterminal xtc.lang.TypedLambda.BasicType.
0728: *
0729: * @param yyStart The index.
0730: * @return The result.
0731: * @throws IOException Signals an I/O error.
0732: */
0733: private Result pBasicType(final int yyStart) throws IOException {
0734: TypedLambdaParserColumn yyColumn = (TypedLambdaParserColumn) column(yyStart);
0735: if (null == yyColumn.fBasicType)
0736: yyColumn.fBasicType = pBasicType$1(yyStart);
0737: return yyColumn.fBasicType;
0738: }
0739:
0740: /** Actually parse xtc.lang.TypedLambda.BasicType. */
0741: private Result pBasicType$1(final int yyStart) throws IOException {
0742: int yyC;
0743: int yyIndex;
0744: Result yyResult;
0745: Node yyValue;
0746: ParseError yyError = ParseError.DUMMY;
0747:
0748: // Alternative 1.
0749:
0750: yyC = character(yyStart);
0751: if ('i' == yyC) {
0752: yyIndex = yyStart + 1;
0753:
0754: yyC = character(yyIndex);
0755: if ('n' == yyC) {
0756: yyIndex = yyIndex + 1;
0757:
0758: yyC = character(yyIndex);
0759: if ('t' == yyC) {
0760: yyIndex = yyIndex + 1;
0761:
0762: yyResult = pSpacing(yyIndex);
0763: yyError = yyResult.select(yyError);
0764: if (yyResult.hasValue()) {
0765:
0766: yyValue = GNode.create("IntegerType", false);
0767: yyValue.setLocation(location(yyStart));
0768:
0769: return yyResult.createValue(yyValue, yyError);
0770: }
0771: }
0772: }
0773: }
0774:
0775: // Alternative 2.
0776:
0777: yyC = character(yyStart);
0778: if ('s' == yyC) {
0779: yyIndex = yyStart + 1;
0780:
0781: yyC = character(yyIndex);
0782: if ('t' == yyC) {
0783: yyIndex = yyIndex + 1;
0784:
0785: yyC = character(yyIndex);
0786: if ('r' == yyC) {
0787: yyIndex = yyIndex + 1;
0788:
0789: yyC = character(yyIndex);
0790: if ('i' == yyC) {
0791: yyIndex = yyIndex + 1;
0792:
0793: yyC = character(yyIndex);
0794: if ('n' == yyC) {
0795: yyIndex = yyIndex + 1;
0796:
0797: yyC = character(yyIndex);
0798: if ('g' == yyC) {
0799: yyIndex = yyIndex + 1;
0800:
0801: yyResult = pSpacing(yyIndex);
0802: yyError = yyResult.select(yyError);
0803: if (yyResult.hasValue()) {
0804:
0805: yyValue = GNode.create(
0806: "StringType", false);
0807: yyValue
0808: .setLocation(location(yyStart));
0809:
0810: return yyResult.createValue(
0811: yyValue, yyError);
0812: }
0813: }
0814: }
0815: }
0816: }
0817: }
0818: }
0819:
0820: // Alternative <Parenthesized>.
0821:
0822: yyResult = pOPEN(yyStart);
0823: yyError = yyResult.select(yyError);
0824: if (yyResult.hasValue()) {
0825:
0826: yyResult = pFunctionType(yyResult.index);
0827: yyError = yyResult.select(yyError);
0828: if (yyResult.hasValue()) {
0829: yyValue = yyResult.semanticValue();
0830:
0831: yyResult = pCLOSE(yyResult.index);
0832: yyError = yyResult.select(yyError);
0833: if (yyResult.hasValue()) {
0834:
0835: return yyResult.createValue(yyValue, yyError);
0836: }
0837: }
0838: }
0839:
0840: // Done.
0841: yyError = yyError.select("basic type expected", yyStart);
0842: return yyError;
0843: }
0844:
0845: // =========================================================================
0846:
0847: /**
0848: * Parse nonterminal xtc.lang.TypedLambda.LAMBDA.
0849: *
0850: * @param yyStart The index.
0851: * @return The result.
0852: * @throws IOException Signals an I/O error.
0853: */
0854: private Result pLAMBDA(final int yyStart) throws IOException {
0855: int yyC;
0856: int yyIndex;
0857: Result yyResult;
0858: Void yyValue;
0859: ParseError yyError = ParseError.DUMMY;
0860:
0861: // Alternative 1.
0862:
0863: yyC = character(yyStart);
0864: if ('\\' == yyC) {
0865: yyIndex = yyStart + 1;
0866:
0867: yyResult = pSpacing(yyIndex);
0868: yyError = yyResult.select(yyError);
0869: if (yyResult.hasValue()) {
0870:
0871: yyValue = null;
0872:
0873: return yyResult.createValue(yyValue, yyError);
0874: }
0875: }
0876:
0877: // Done.
0878: yyError = yyError.select("LAMBDA expected", yyStart);
0879: return yyError;
0880: }
0881:
0882: // =========================================================================
0883:
0884: /**
0885: * Parse nonterminal xtc.lang.TypedLambda.COLON.
0886: *
0887: * @param yyStart The index.
0888: * @return The result.
0889: * @throws IOException Signals an I/O error.
0890: */
0891: private Result pCOLON(final int yyStart) throws IOException {
0892: int yyC;
0893: int yyIndex;
0894: Result yyResult;
0895: Void yyValue;
0896: ParseError yyError = ParseError.DUMMY;
0897:
0898: // Alternative 1.
0899:
0900: yyC = character(yyStart);
0901: if (':' == yyC) {
0902: yyIndex = yyStart + 1;
0903:
0904: yyResult = pSpacing(yyIndex);
0905: yyError = yyResult.select(yyError);
0906: if (yyResult.hasValue()) {
0907:
0908: yyValue = null;
0909:
0910: return yyResult.createValue(yyValue, yyError);
0911: }
0912: }
0913:
0914: // Done.
0915: yyError = yyError.select("COLON expected", yyStart);
0916: return yyError;
0917: }
0918:
0919: // =========================================================================
0920:
0921: /**
0922: * Parse nonterminal xtc.lang.TypedLambda.DOT.
0923: *
0924: * @param yyStart The index.
0925: * @return The result.
0926: * @throws IOException Signals an I/O error.
0927: */
0928: private Result pDOT(final int yyStart) throws IOException {
0929: int yyC;
0930: int yyIndex;
0931: Result yyResult;
0932: Void yyValue;
0933: ParseError yyError = ParseError.DUMMY;
0934:
0935: // Alternative 1.
0936:
0937: yyC = character(yyStart);
0938: if ('.' == yyC) {
0939: yyIndex = yyStart + 1;
0940:
0941: yyResult = pSpacing(yyIndex);
0942: yyError = yyResult.select(yyError);
0943: if (yyResult.hasValue()) {
0944:
0945: yyValue = null;
0946:
0947: return yyResult.createValue(yyValue, yyError);
0948: }
0949: }
0950:
0951: // Done.
0952: yyError = yyError.select("DOT expected", yyStart);
0953: return yyError;
0954: }
0955:
0956: // =========================================================================
0957:
0958: /**
0959: * Parse nonterminal xtc.lang.TypedLambda.ARROW.
0960: *
0961: * @param yyStart The index.
0962: * @return The result.
0963: * @throws IOException Signals an I/O error.
0964: */
0965: private Result pARROW(final int yyStart) throws IOException {
0966: int yyC;
0967: int yyIndex;
0968: Result yyResult;
0969: Void yyValue;
0970: ParseError yyError = ParseError.DUMMY;
0971:
0972: // Alternative 1.
0973:
0974: yyC = character(yyStart);
0975: if ('-' == yyC) {
0976: yyIndex = yyStart + 1;
0977:
0978: yyC = character(yyIndex);
0979: if ('>' == yyC) {
0980: yyIndex = yyIndex + 1;
0981:
0982: yyResult = pSpacing(yyIndex);
0983: yyError = yyResult.select(yyError);
0984: if (yyResult.hasValue()) {
0985:
0986: yyValue = null;
0987:
0988: return yyResult.createValue(yyValue, yyError);
0989: }
0990: }
0991: }
0992:
0993: // Done.
0994: yyError = yyError.select("ARROW expected", yyStart);
0995: return yyError;
0996: }
0997:
0998: // =========================================================================
0999:
1000: /**
1001: * Parse nonterminal xtc.lang.TypedLambda.OPEN.
1002: *
1003: * @param yyStart The index.
1004: * @return The result.
1005: * @throws IOException Signals an I/O error.
1006: */
1007: private Result pOPEN(final int yyStart) throws IOException {
1008: TypedLambdaParserColumn yyColumn = (TypedLambdaParserColumn) column(yyStart);
1009: if (null == yyColumn.fOPEN)
1010: yyColumn.fOPEN = pOPEN$1(yyStart);
1011: return yyColumn.fOPEN;
1012: }
1013:
1014: /** Actually parse xtc.lang.TypedLambda.OPEN. */
1015: private Result pOPEN$1(final int yyStart) throws IOException {
1016: int yyC;
1017: int yyIndex;
1018: Result yyResult;
1019: Void yyValue;
1020: ParseError yyError = ParseError.DUMMY;
1021:
1022: // Alternative 1.
1023:
1024: yyC = character(yyStart);
1025: if ('(' == yyC) {
1026: yyIndex = yyStart + 1;
1027:
1028: yyResult = pSpacing(yyIndex);
1029: yyError = yyResult.select(yyError);
1030: if (yyResult.hasValue()) {
1031:
1032: yyValue = null;
1033:
1034: return yyResult.createValue(yyValue, yyError);
1035: }
1036: }
1037:
1038: // Done.
1039: yyError = yyError.select("OPEN expected", yyStart);
1040: return yyError;
1041: }
1042:
1043: // =========================================================================
1044:
1045: /**
1046: * Parse nonterminal xtc.lang.TypedLambda.CLOSE.
1047: *
1048: * @param yyStart The index.
1049: * @return The result.
1050: * @throws IOException Signals an I/O error.
1051: */
1052: private Result pCLOSE(final int yyStart) throws IOException {
1053: TypedLambdaParserColumn yyColumn = (TypedLambdaParserColumn) column(yyStart);
1054: if (null == yyColumn.fCLOSE)
1055: yyColumn.fCLOSE = pCLOSE$1(yyStart);
1056: return yyColumn.fCLOSE;
1057: }
1058:
1059: /** Actually parse xtc.lang.TypedLambda.CLOSE. */
1060: private Result pCLOSE$1(final int yyStart) throws IOException {
1061: int yyC;
1062: int yyIndex;
1063: Result yyResult;
1064: Void yyValue;
1065: ParseError yyError = ParseError.DUMMY;
1066:
1067: // Alternative 1.
1068:
1069: yyC = character(yyStart);
1070: if (')' == yyC) {
1071: yyIndex = yyStart + 1;
1072:
1073: yyResult = pSpacing(yyIndex);
1074: yyError = yyResult.select(yyError);
1075: if (yyResult.hasValue()) {
1076:
1077: yyValue = null;
1078:
1079: return yyResult.createValue(yyValue, yyError);
1080: }
1081: }
1082:
1083: // Done.
1084: yyError = yyError.select("CLOSE expected", yyStart);
1085: return yyError;
1086: }
1087:
1088: // =========================================================================
1089:
1090: /**
1091: * Parse nonterminal xtc.util.Spacing.Spacing.
1092: *
1093: * @param yyStart The index.
1094: * @return The result.
1095: * @throws IOException Signals an I/O error.
1096: */
1097: private Result pSpacing(final int yyStart) throws IOException {
1098: int yyC;
1099: int yyIndex;
1100: Result yyPredResult;
1101: boolean yyPredMatched;
1102: int yyBase;
1103: int yyRepetition1;
1104: int yyRepetition2;
1105: Void yyValue;
1106: ParseError yyError = ParseError.DUMMY;
1107:
1108: // Alternative 1.
1109:
1110: yyRepetition1 = yyStart;
1111: while (true) {
1112:
1113: final int yyChoice1 = yyRepetition1;
1114:
1115: // Nested alternative 1.
1116:
1117: yyC = character(yyChoice1);
1118: if (-1 != yyC) {
1119: yyIndex = yyChoice1 + 1;
1120:
1121: switch (yyC) {
1122: case ' ': {
1123: yyRepetition1 = yyIndex;
1124: continue;
1125: }
1126:
1127: case '\t': {
1128: yyRepetition1 = yyIndex;
1129: continue;
1130: }
1131:
1132: case '\f': {
1133: yyRepetition1 = yyIndex;
1134: continue;
1135: }
1136:
1137: case '\r': {
1138: final int yyChoice2 = yyIndex;
1139:
1140: // Nested alternative 1.
1141:
1142: yyC = character(yyChoice2);
1143: if ('\n' == yyC) {
1144: yyIndex = yyChoice2 + 1;
1145:
1146: yyRepetition1 = yyIndex;
1147: continue;
1148: }
1149:
1150: // Nested alternative 2.
1151:
1152: yyRepetition1 = yyChoice2;
1153: continue;
1154: }
1155:
1156: case '\n': {
1157: yyRepetition1 = yyIndex;
1158: continue;
1159: }
1160:
1161: case '/': {
1162: yyC = character(yyIndex);
1163: if (-1 != yyC) {
1164: yyIndex = yyIndex + 1;
1165:
1166: switch (yyC) {
1167: case '*': {
1168: yyRepetition2 = yyIndex;
1169: while (true) {
1170:
1171: final int yyChoice2 = yyRepetition2;
1172:
1173: // Nested alternative 1.
1174:
1175: yyC = character(yyChoice2);
1176: if (-1 != yyC) {
1177: yyIndex = yyChoice2 + 1;
1178:
1179: switch (yyC) {
1180: case '*': {
1181: yyPredMatched = false;
1182:
1183: yyC = character(yyIndex);
1184: if ('/' == yyC) {
1185:
1186: yyPredMatched = true;
1187: }
1188:
1189: if (!yyPredMatched) {
1190:
1191: yyRepetition2 = yyIndex;
1192: continue;
1193: } else {
1194: yyError = yyError.select(
1195: "spacing expected",
1196: yyStart);
1197: }
1198: }
1199: break;
1200:
1201: default: {
1202: yyRepetition2 = yyIndex;
1203: continue;
1204: }
1205: }
1206: }
1207: break;
1208: }
1209:
1210: yyBase = yyRepetition2;
1211: yyC = character(yyBase);
1212: if ('*' == yyC) {
1213: yyIndex = yyRepetition2 + 1;
1214:
1215: yyC = character(yyIndex);
1216: if ('/' == yyC) {
1217: yyIndex = yyIndex + 1;
1218:
1219: yyRepetition1 = yyIndex;
1220: continue;
1221: } else {
1222: yyError = yyError.select(
1223: "\"*/\" expected", yyBase);
1224: }
1225: } else {
1226: yyError = yyError.select(
1227: "\"*/\" expected", yyBase);
1228: }
1229: }
1230: break;
1231:
1232: case '/': {
1233: yyRepetition2 = yyIndex;
1234: while (true) {
1235:
1236: yyC = character(yyRepetition2);
1237: if (-1 != yyC) {
1238: yyIndex = yyRepetition2 + 1;
1239:
1240: switch (yyC) {
1241: case '\n':
1242: case '\r':
1243: /* No match. */
1244: break;
1245:
1246: default: {
1247: yyRepetition2 = yyIndex;
1248: continue;
1249: }
1250: }
1251: }
1252: break;
1253: }
1254:
1255: final int yyChoice2 = yyRepetition2;
1256:
1257: // Nested alternative 1.
1258:
1259: yyC = character(yyChoice2);
1260: if (-1 != yyC) {
1261: yyIndex = yyChoice2 + 1;
1262:
1263: switch (yyC) {
1264: case '\r': {
1265: final int yyChoice3 = yyIndex;
1266:
1267: // Nested alternative 1.
1268:
1269: yyC = character(yyChoice3);
1270: if ('\n' == yyC) {
1271: yyIndex = yyChoice3 + 1;
1272:
1273: yyRepetition1 = yyIndex;
1274: continue;
1275: }
1276:
1277: // Nested alternative 2.
1278:
1279: yyRepetition1 = yyChoice3;
1280: continue;
1281: }
1282:
1283: case '\n': {
1284: yyRepetition1 = yyIndex;
1285: continue;
1286: }
1287:
1288: default:
1289: /* No match. */
1290: }
1291: }
1292:
1293: // Nested alternative 2.
1294:
1295: yyPredResult = pEndOfFile(yyChoice2);
1296: yyError = yyPredResult.select(yyError);
1297: if (yyPredResult.hasValue()) {
1298:
1299: yyRepetition1 = yyChoice2;
1300: continue;
1301: }
1302: }
1303: break;
1304:
1305: default:
1306: /* No match. */
1307: }
1308: }
1309: }
1310: break;
1311:
1312: default:
1313: /* No match. */
1314: }
1315: }
1316: break;
1317: }
1318:
1319: yyValue = null;
1320:
1321: return new SemanticValue(yyValue, yyRepetition1, yyError);
1322: }
1323:
1324: // =========================================================================
1325:
1326: /**
1327: * Parse nonterminal xtc.util.Spacing.EndOfFile.
1328: *
1329: * @param yyStart The index.
1330: * @return The result.
1331: * @throws IOException Signals an I/O error.
1332: */
1333: private Result pEndOfFile(final int yyStart) throws IOException {
1334: int yyC;
1335: boolean yyPredMatched;
1336: Void yyValue;
1337: ParseError yyError = ParseError.DUMMY;
1338:
1339: // Alternative 1.
1340:
1341: yyPredMatched = false;
1342:
1343: yyC = character(yyStart);
1344: if (-1 != yyC) {
1345:
1346: yyPredMatched = true;
1347: }
1348:
1349: if (!yyPredMatched) {
1350:
1351: yyValue = null;
1352:
1353: return new SemanticValue(yyValue, yyStart, yyError);
1354: } else {
1355: yyError = yyError.select("end of file expected", yyStart);
1356: }
1357:
1358: // Done.
1359: return yyError;
1360: }
1361:
1362: // =========================================================================
1363:
1364: /**
1365: * Get the specified text.
1366: *
1367: * @param s The text.
1368: * @return The text.
1369: */
1370: protected static final String toText(String s) {
1371: return s;
1372: }
1373:
1374: // =========================================================================
1375:
1376: /**
1377: * Parse the specified files.
1378: *
1379: * @param args The file names.
1380: */
1381: public static void main(String[] args) {
1382: if ((null == args) || (0 == args.length)) {
1383: System.err.println("Usage: <file-name>+");
1384:
1385: } else {
1386: for (int i = 0; i < args.length; i++) {
1387: System.err.println("Processing " + args[i] + " ...");
1388:
1389: Reader in = null;
1390: try {
1391: in = new BufferedReader(new FileReader(args[i]));
1392: TypedLambdaParser p = new TypedLambdaParser(in,
1393: args[i], (int) new File(args[i]).length());
1394: Result r = p.pExpression(0);
1395:
1396: if (r.hasValue()) {
1397: SemanticValue v = (SemanticValue) r;
1398:
1399: if (v.value instanceof Node) {
1400: Printer ptr = new Printer(
1401: new BufferedWriter(
1402: new OutputStreamWriter(
1403: System.out)));
1404: ptr.format((Node) v.value).pln().flush();
1405: } else {
1406: System.out.println(v.value.toString());
1407: }
1408:
1409: } else {
1410: ParseError err = (ParseError) r;
1411: if (-1 == err.index) {
1412: System.err.println(" Parse error");
1413: } else {
1414: System.err.println(" "
1415: + p.location(err.index) + ": "
1416: + err.msg);
1417: }
1418: }
1419:
1420: } catch (Throwable x) {
1421: while (null != x.getCause()) {
1422: x = x.getCause();
1423: }
1424: x.printStackTrace();
1425: } finally {
1426: try {
1427: in.close();
1428: } catch (Throwable x) {
1429: /* Ignore. */
1430: }
1431: }
1432: }
1433: }
1434: }
1435:
1436: }
|