@Override public int getCharEndIndex(Object impl) { SourceSection section = (SourceSection) impl; return section.getCharEndIndex(); }
@Override public int getCharEndIndex(Object impl) { SourceSection section = (SourceSection) impl; return section.getCharEndIndex(); }
@Override public int getOffsetEnd() { return sourceSection.getCharEndIndex(); }
/** Get the last character index (inclusive). */ private static int getCharEndIndex(SourceSection ss) { if (ss.getCharLength() > 0) { return ss.getCharEndIndex() - 1; } else { return ss.getCharIndex(); } }
/** Get the last character index (inclusive). */ private static int getCharEndIndex(SourceSection ss) { if (ss.getCharLength() > 0) { return ss.getCharEndIndex() - 1; } else { return ss.getCharIndex(); } }
private static String displaySourceAttribution(Node node) { final SourceSection section = node.getSourceSection(); if (section == null) { return ""; } if (section.getSource() == null) { // TODO we can remove this block if SourceSection#createUnavailable was removed, because // then source cannot become null anymore. return "source: <unknown>"; } final String srcText = section.getCharacters().toString(); final StringBuilder sb = new StringBuilder(); sb.append("source:"); sb.append(" (" + section.getCharIndex() + "," + (section.getCharEndIndex() - 1) + ")"); sb.append(" line=" + section.getStartLine()); sb.append(" len=" + srcText.length()); sb.append(" text=\"" + srcText + "\""); return sb.toString(); }
private static String displaySourceAttribution(Node node) { final SourceSection section = node.getSourceSection(); if (section == null) { return ""; } if (section.getSource() == null) { // TODO we can remove this block if SourceSection#createUnavailable was removed, because // then source cannot become null anymore. return "source: <unknown>"; } final String srcText = section.getCharacters().toString(); final StringBuilder sb = new StringBuilder(); sb.append("source:"); sb.append(" (" + section.getCharIndex() + "," + (section.getCharEndIndex() - 1) + ")"); sb.append(" line=" + section.getStartLine()); sb.append(" len=" + srcText.length()); sb.append(" text=\"" + srcText + "\""); return sb.toString(); }
/** * Returns the source code fragment described by this section. Returns an empty character * sequence for out of bounds or {@link #isAvailable() unavailable} source sections. Please note * that calling this method causes the {@link Source#getCharacters() code} of the * {@link #getSource() source} to be loaded if it was not yet loaded. * * @return the code as a CharSequence * @since 0.28 */ public CharSequence getCharacters() { if (!isValid()) { return ""; } return source.getCharacters().subSequence(getCharIndex(), getCharEndIndex()); }
private static String displaySourceAttribution(Node node) { final SourceSection section = node.getSourceSection(); if (section instanceof NullSourceSection) { return "source: " + section.getShortDescription(); } if (section != null) { final String srcText = section.getCode(); final StringBuilder sb = new StringBuilder(); sb.append("source:"); sb.append(" (" + section.getCharIndex() + "," + (section.getCharEndIndex() - 1) + ")"); sb.append(" line=" + section.getLineLocation().getLineNumber()); sb.append(" len=" + srcText.length()); sb.append(" text=\"" + srcText + "\""); return sb.toString(); } return ""; }
@TruffleBoundary private JsonArray sourceSectionsToJson() { return Json.array(getStateSet().stream().map(RegexASTNode::getSourceSection).filter(Objects::nonNull).map(x -> Json.obj( Json.prop("start", x.getCharIndex()), Json.prop("end", x.getCharEndIndex())))); }
private void findOffsetApproximation(InstrumentableNode node, SourceSection sourceSection, int o1, int o2) { if (o1 <= offset && offset <= o2) { // Exact match. There can be more of these, find the smallest one: if (containsMatch == null || containsMatch.getCharLength() > sourceSection.getCharLength()) { containsMatch = sourceSection; containsNode = node; } } else if (o2 < offset) { // Previous match. Find the nearest one (with the largest end index): if (previousMatch == null || previousMatch.getCharEndIndex() < sourceSection.getCharEndIndex() || // when equal end, find the largest one previousMatch.getCharEndIndex() == sourceSection.getCharEndIndex() && previousMatch.getCharLength() < sourceSection.getCharLength()) { previousMatch = sourceSection; previousNode = node; } } else { assert offset < o1; // Next match. Find the nearest one (with the smallest start index): if (nextMatch == null || nextMatch.getCharIndex() > sourceSection.getCharIndex() || // when equal start, find the largest one nextMatch.getCharIndex() == sourceSection.getCharIndex() && nextMatch.getCharLength() < sourceSection.getCharLength()) { nextMatch = sourceSection; nextNode = node; } } }
@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(); }
@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(); }
private void computeRootBits(SourceSection sourceSection) { int bits = computingRootNodeBits; if (RootNodeBits.isUninitialized(bits)) { return; } if (sourceSection != null) { if (RootNodeBits.isNoSourceSection(bits)) { bits = RootNodeBits.setHasSourceSection(bits); } if (rootSourceSection != null) { if (RootNodeBits.isSourceSectionsHierachical(bits)) { if (sourceSection.getCharIndex() < rootSourceSection.getCharIndex() // || sourceSection.getCharEndIndex() > rootSourceSection.getCharEndIndex()) { bits = RootNodeBits.setSourceSectionsUnstructured(bits); } } if (RootNodeBits.isSameSource(bits) && rootSourceSection.getSource() != sourceSection.getSource()) { bits = RootNodeBits.setHasDifferentSource(bits); } } else { bits = RootNodeBits.setSourceSectionsUnstructured(bits); bits = RootNodeBits.setHasDifferentSource(bits); } } computingRootNodeBits = bits; }
private void computeRootBits(SourceSection sourceSection) { int bits = computingRootNodeBits; if (RootNodeBits.isUninitialized(bits)) { return; } if (sourceSection != null) { if (RootNodeBits.isNoSourceSection(bits)) { bits = RootNodeBits.setHasSourceSection(bits); } if (rootSourceSection != null) { if (RootNodeBits.isSourceSectionsHierachical(bits)) { if (sourceSection.getCharIndex() < rootSourceSection.getCharIndex() // || sourceSection.getCharEndIndex() > rootSourceSection.getCharEndIndex()) { bits = RootNodeBits.setSourceSectionsUnstructured(bits); } } if (RootNodeBits.isSameSource(bits) && rootSourceSection.getSource() != sourceSection.getSource()) { bits = RootNodeBits.setHasDifferentSource(bits); } } else { bits = RootNodeBits.setSourceSectionsUnstructured(bits); bits = RootNodeBits.setHasDifferentSource(bits); } } computingRootNodeBits = bits; }
@TruffleBoundary private JsonArray sourceSectionsToJson() { if (!groupBoundaries.hasIndexUpdates()) { return Json.array(); } return Json.array(groupBoundaries.getUpdateIndices().stream().mapToObj(x -> { Group group = source.getStateSet().getAst().getGroupByBoundaryIndex(x); SourceSection sourceSection = (x & 1) == 0 ? group.getSourceSectionBegin() : group.getSourceSectionEnd(); return Json.obj(Json.prop("start", sourceSection.getCharIndex()), Json.prop("end", sourceSection.getCharEndIndex())); })); }
InstrumentableNode getContainsNode() { if (containsNode == null) { return null; } if (line > 0) { if (anchor == SuspendAnchor.BEFORE && line == containsMatch.getStartLine() || anchor == SuspendAnchor.AFTER && line == containsMatch.getEndLine()) { return (InstrumentableNode) containsNode.getOuter(containsMatch.getCharLength()); } } else { if (anchor == SuspendAnchor.BEFORE && offset == containsMatch.getCharIndex() || anchor == SuspendAnchor.AFTER && offset == containsMatch.getCharEndIndex() - 1) { return (InstrumentableNode) containsNode.getOuter(containsMatch.getCharLength()); } } return (InstrumentableNode) containsNode.getInner(containsMatch.getCharLength()); }
@Override public void onLoad(LoadSourceSectionEvent event) { Node eventNode = event.getNode(); if (!(eventNode instanceof InstrumentableNode && ((InstrumentableNode) eventNode).isInstrumentable())) { return; } InstrumentableNode node = (InstrumentableNode) eventNode; SourceSection sourceSection = event.getSourceSection(); if (matchSectionLine(node, sourceSection)) { // We have exact line match, we do not need to do anything more return; } int o1 = sourceSection.getCharIndex(); int o2; if (sourceSection.getCharLength() > 0) { o2 = sourceSection.getCharEndIndex() - 1; } else { o2 = sourceSection.getCharIndex(); } if (matchSectionOffset(node, sourceSection, o1, o2)) { // We have exact offset index match, we do not need to do anything more return; } // Offset approximation findOffsetApproximation(node, sourceSection, o1, o2); }