/** * Parse the supplied DDL content and return the {@link AstNode root node} of the AST * representation. * * @param ddl content string; may not be null * @param fileName the approximate name of the file containing the DDL content; may be null if * this is not known * @return the root tree {@link AstNode} * @throws ParsingException if there is an error parsing the supplied DDL content */ public AstNode parse(String ddl, String fileName) throws ParsingException { assert ddl != null; // Go through each parser and score the DDL content ... List<ScoredParser> scoredParsers = new LinkedList<ScoredParser>(); for (DdlParser parser : parsers) { DdlParserScorer scorer = new DdlParserScorer(); try { Object scoreResult = parser.score(ddl, fileName, scorer); scoredParsers.add(new ScoredParser(parser, scorer, scoreResult)); } catch (Throwable t) { // Continue ... } } if (!scoredParsers.isEmpty()) { Collections.sort(scoredParsers); } else { // Just keep the parsers in order ... for (DdlParser parser : parsers) { scoredParsers.add(new ScoredParser(parser, new DdlParserScorer(), null)); } } // Go through each of the parsers (in order) to parse the content. Start with the first one // and return if there is no parsing failure; otherwise, just continue with the next parser // until no failure ... AstNode astRoot = null; RuntimeException firstException = null; for (ScoredParser scoredParser : scoredParsers) { try { astRoot = nodeFactory.node(StandardDdlLexicon.STATEMENTS_CONTAINER); astRoot.setProperty(JcrConstants.JCR_PRIMARY_TYPE, JcrConstants.NT_UNSTRUCTURED); DdlParser parser = scoredParser.getParser(); parser.parse(ddl, astRoot, scoredParser.getScoringResult()); astRoot.setProperty(StandardDdlLexicon.PARSER_ID, parser.getId()); // Success, so return the resulting AST ... return astRoot; } catch (ParsingException t) { if (firstException == null) firstException = t; // Continue ... } catch (RuntimeException t) { if (firstException == null) firstException = t; // Continue ... } } if (firstException != null) { throw firstException; } // No exceptions, but nothing was found ... throw new ParsingException( new Position(-1, 1, 0), DdlSequencerI18n.errorParsingDdlContent.text()); }
/** * {@inheritDoc} * * @see java.lang.Comparable#compareTo(java.lang.Object) */ @Override public int compareTo(ScoredParser that) { if (that == null) return 1; return that.getScorer().getScore() - this.getScorer().getScore(); }