@Override public String toString() { return getPath().toString(); }
public MatcherPath getPath() { if (path == null) { path = new MatcherPath(new MatcherPath.Element(matcher, startIndex, level), parent != null ? parent.getPath() : null); } return path; }
public boolean match(MatcherContext<?> context) { boolean matched = inner == null && context.getMatcher().match(context) || inner != null && inner.match(context); if (context.getCurrentIndex() == errorIndex) { if (matched && seeking) { seeking = false; } if (!matched && !seeking && context.getMatcher().accept(isSingleCharMatcherVisitor)) { failedMatchers.add(context.getPath()); } } return matched; } }
@Override public boolean match(final MatcherContext<?> context) { boolean matched = inner == null && context.getMatcher().match(context) || inner != null && inner.match(context); if (context.getCurrentIndex() == errorIndex) { if (matched && seeking) { seeking = false; } Matcher matcher = context.getMatcher(); if (!matched && !seeking && matcher != null && matcher.accept(isSingleCharMatcherVisitor)) { failedMatchers.add(context.getPath()); } } return matched; } }
public boolean runMatcher() { try { if (matchHandler.match(this)) { if (parent != null) { parent.currentIndex = currentIndex; parent.currentChar = currentChar; } matcher = null; // "retire" this context return true; } matcher = null; // "retire" this context until is "activated" again by a getSubContext(...) on the parent return false; } catch (ParserRuntimeException e) { throw e; // don't wrap, just bubble up } catch (RecoveringParseRunner.TimeoutException e) { throw e; // don't wrap, just bubble up } catch (Throwable e) { throw new ParserRuntimeException(e, printParseError(new BasicParseError(inputBuffer, currentIndex, StringUtils.escape(String.format("Error while parsing %s '%s' at input position", matcher instanceof ActionMatcher ? "action" : "rule", getPath())))) + '\n' + e); } } }
private void print(MatcherContext<?> context, boolean matched) { Position pos = context.getInputBuffer().getPosition(context.getCurrentIndex()); MatcherPath path = context.getPath(); MatcherPath prefix = lastPath != null ? path.commonPrefix(lastPath) : null; if (prefix != null && prefix.length() > 1) getLog().receive("..(" + (prefix.length() - 1) + ")../"); getLog().receive(path.toString(prefix != null ? prefix.parent : null)); String line = context.getInputBuffer().extractLine(pos.line); getLog().receive(", " + (matched ? "matched" : "failed") + ", cursor at " + pos.line + ':' + pos.column + " after \"" + line.substring(0, Math.min(line.length(), pos.column - 1)) + "\"\n"); lastPath = path; } }
public boolean match(MatcherContext context) { checkArgNotNull(context, "context"); int lastIndex = context.getCurrentIndex(); while (subMatcher.getSubContext(context).runMatcher()) { int currentLocation = context.getCurrentIndex(); if (currentLocation == lastIndex) { throw new GrammarException("The inner rule of ZeroOrMore rule '%s' must not allow empty matches", context.getPath()); } lastIndex = currentLocation; } context.createNode(); return true; }
public boolean match(MatcherContext context) { boolean matched = subMatcher.getSubContext(context).runMatcher(); if (!matched) return false; // collect all further matches as well int lastIndex = context.getCurrentIndex(); while (subMatcher.getSubContext(context).runMatcher()) { int currentIndex = context.getCurrentIndex(); if (currentIndex == lastIndex) { throw new GrammarException("The inner rule of OneOrMore rule '%s' must not allow empty matches", context.getPath()); } lastIndex = currentIndex; } context.createNode(); return true; }
@SuppressWarnings({"unchecked"}) public <V> boolean match(MatcherContext<V> context) { if (skipInPredicates && context.inPredicate()) return true; // actions need to run in the parent context MatcherContext parentContext = context.getParent(); if (!contextAwares.isEmpty()) { for (ContextAware contextAware : contextAwares) { contextAware.setContext(parentContext); } } try { Object valueStackSnapshot = context.getValueStack().takeSnapshot(); if (!action.run(parentContext)) { // failing actions are not allowed to change the ValueStack context.getValueStack().restoreSnapshot(valueStackSnapshot); return false; } // since we initialize the actions own context only partially in getSubContext(MatcherContext) // (in order to be able to still access the previous subcontexts fields in action expressions) // we need to make sure to not accidentally advance the current index of our parent with some old // index from a previous subcontext, so we explicitly set the marker here context.setCurrentIndex(parentContext.getCurrentIndex()); return true; } catch (ActionException e) { context.getParseErrors().add(new ActionError(context.getInputBuffer(), context.getCurrentIndex(), e.getMessage(), context.getPath(), e)); return false; } }