/** * Sets the {@link com.oracle.truffle.api.source.SourceSection} of a given {@link Token} in * respect of {@link RegexSource#getSource()}. * * @param startIndex inclusive start index of the source section in respect of * {@link RegexSource#getPattern()}. * @param endIndex exclusive end index of the source section in respect of * {@link RegexSource#getPattern()}. */ private void setSourceSection(Token t, int startIndex, int endIndex) { if (options.isDumpAutomata()) { // RegexSource#getSource() prepends a slash ('/') to the pattern, so we have to add an // offset of 1 here. t.setSourceSection(source.getSource().createSection(startIndex + 1, endIndex - startIndex)); } }
/** * Creates a representation of a line of text in the source identified only by line number, from * which the character information will be computed. * * @param identifier terse description of the line * @param lineNumber 1-based line number of the first character in the section * @return newly created object representing the specified line * @throws IllegalArgumentException if the line does not exist the source * @throws IllegalStateException if the source is one of the "null" instances */ public final SourceSection createSection(String identifier, int lineNumber) { checkTextMap(); final int charIndex = textMap.lineStartOffset(lineNumber); final int length = textMap.lineLength(lineNumber); return createSection(identifier, charIndex, length); }
@CompilerDirectives.TruffleBoundary @Override public SourceSection getSourceSection() { if (sourceSection == null) { String patternSrc = "/" + source.getPattern() + "/" + source.getFlags(); Source src = Source.newBuilder(RegexLanguage.ID, patternSrc, source.getPattern()).mimeType("application/js-regex").build(); sourceSection = src.createSection(0, patternSrc.length()); } return sourceSection; }
private SourceSection createSourceSection(ParserRuleContext ctx) { return source.createSection(MumblerParser.VOCABULARY.getDisplayName(ctx.getRuleIndex()), ctx.start.getLine(), ctx.start.getCharPositionInLine(), ctx.stop.getStopIndex() - ctx.start.getStartIndex()); }
return rSource.createUnavailableSection(); } else if (section.hasCharIndex()) { return rSource.createSection(section.getCharIndex(), section.getCharLength()); } else if (section.hasColumns()) { return rSource.createSection(section.getStartLine(), section.getStartColumn(), section.getEndLine(), section.getEndColumn()); } else if (section.hasLines()) { startColumn = 0; return rSource.createSection(startLine, startColumn + 1, endLine, rSource.getLineLength(endLine)); } else { return section;
@Override public SourceSection getSourceSection() { if (super.getSourceSection() == null && sourceSectionBegin != null && sourceSectionEnd != null) { super.setSourceSection(sourceSectionBegin.getSource().createSection(sourceSectionBegin.getCharIndex(), sourceSectionEnd.getCharEndIndex() - sourceSectionBegin.getCharIndex())); } return super.getSourceSection(); }
/** * Adds a new {@link Sequence} to the current {@link Group}. * * @param token the opening bracket of the parent group ({@link Token.Kind#captureGroupBegin}) * or the alternation symbol ({@link Token.Kind#alternation}) that opens the new * sequence. */ private void addSequence(Token token) { if (!curGroup.getAlternatives().isEmpty()) { setComplexLookAround(); } curSequence = curGroup.addSequence(ast); if (options.isDumpAutomata()) { if (token != null) { SourceSection src = token.getSourceSection(); // set source section to empty string, it will be updated by the Sequence object // when new Terms are added to it curSequence.setSourceSection(src.getSource().createSection(src.getCharEndIndex(), 0)); } } curTerm = null; }
@Override public SourceSection getSourceSection() { if (super.getSourceSection() == null) { RegexASTSubtreeRootNode parent = getSubTreeParent(); if (parent == null || parent.getSourceSection() == null) { // initial state, not part of actual AST return null; } // set source section to empty space after parent tree SourceSection parentSourceSection = parent.getSourceSection(); super.setSourceSection(parentSourceSection.getSource().createSection(parentSourceSection.getCharEndIndex(), 0)); } return super.getSourceSection(); }
@Override public SourceSection getSourceSection() { SourceSection src = super.getSourceSection(); if (src != null && !isEmpty()) { int endIndex = getLastTerm().getSourceSection().getCharEndIndex(); if (endIndex != src.getCharEndIndex()) { int startIndex = src.getCharIndex(); super.setSourceSection(src.getSource().createSection(startIndex, endIndex - startIndex)); } } return super.getSourceSection(); }
/** * This method should be called when {@code curSequence} is about to be closed. If the current * {@link Sequence} <em>and</em> the last {@link Sequence} consist of a single * {@link CharacterClass} each, the {@link CharacterClass} contained in the current * {@link Sequence} will be removed and merged into the last {@link Sequence}'s * {@link CharacterClass}, resulting in a smaller NFA. * * @return {@code true} if the {@link CharacterClass} in the current sequence was merged with * the {@link CharacterClass} in the last Sequence. */ private boolean tryMergeSingleCharClassAlternations() { if (curGroup.size() > 1 && curSequence.isSingleCharClass()) { assert curSequence == curGroup.getAlternatives().get(curGroup.size() - 1); Sequence prevSequence = curGroup.getAlternatives().get(curGroup.size() - 2); if (prevSequence.isSingleCharClass()) { CharacterClass prevCC = (CharacterClass) prevSequence.getFirstTerm(); CharacterClass curCC = (CharacterClass) curSequence.getFirstTerm(); prevCC.setMatcherBuilder(prevCC.getMatcherBuilder().union(curCC.getMatcherBuilder())); curSequence.removeLastTerm(); if (options.isDumpAutomata()) { // set source section to cover both char classes and the "|" in between SourceSection prevCCSrc = prevCC.getSourceSection(); prevCC.setSourceSection(prevCCSrc.getSource().createSection( prevCCSrc.getCharIndex(), prevCCSrc.getCharLength() + curCC.getSourceSection().getCharLength() + 1)); } return true; } } return false; }
Group root = createGroup(null, false, rootCapture, rootParent); if (options.isDumpAutomata()) { root.setSourceSectionBegin(ast.getSource().getSource().createSection(0, 1)); root.setSourceSectionEnd(ast.getSource().getSource().createSection(ast.getSource().getPattern().length() + 1, 1));