public boolean match(MatcherContext context) { if (context.getCurrentChar() != character) return false; context.advanceIndex(1); context.createNode(); return true; }
@Override public boolean match(MatcherContext context) { if (!context.fastStringMatching()) { return super.match(context); } if (!context.getInputBuffer().test(context.getCurrentIndex(), characters)) return false; context.advanceIndex(characters.length); context.createNode(); return true; } }
public final MatcherContext<V> getBasicSubContext() { if (subContext == null) { // init new level subContext = new MatcherContext<V>(inputBuffer, valueStack, parseErrors, matchHandler, this, level + 1, fastStringMatching, memoizedMismatches); } else { subContext.path = null; // we always need to reset the MatcherPath, even for actions } return subContext; }
@Override public MatcherContext getSubContext(MatcherContext context) { MatcherContext subContext = context.getBasicSubContext(); subContext.setMatcher(this); if (context.getCurrentIndex() > context.getStartIndex()) { // if we have already matched something we must be in a sequence at the second or later position // the subcontext contains match data that the action might want to access, so we use the existing // subcontext without reinitializing return subContext; } else { return context.getSubContext(this); } }
private boolean notTestNot(MatcherContext context) { return !(context.getMatcher() instanceof TestNotMatcher) && (context.getParent() == null || notTestNot(context.getParent())); } }
@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; } }
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; }
public void addError(String message) { MatcherContext matcherContext = (MatcherContext) getContext(); matcherContext.markError(); List parseErrors = matcherContext.getParseErrors(); parseErrors.add(new BasicParseError(matcherContext.getInputBuffer(), parseErrors.size(), message)); }
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; } }
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) { int lastIndex = context.getCurrentIndex(); Object valueStackSnapshot = context.getValueStack().takeSnapshot(); if (subMatcher.getSubContext(context).runMatcher()) return false; // reset location, Test matchers never advance context.setCurrentIndex(lastIndex); // erase all value stack changes the the submatcher could have made context.getValueStack().restoreSnapshot(valueStackSnapshot); return true; }
public boolean match(MatcherContext context) { Object valueStackSnapshot = context.getValueStack().takeSnapshot(); List<Matcher> children = getChildren(); int size = children.size(); for (int i = 0; i < size; i++) { Matcher matcher = children.get(i); // remember the current index in the context, so we can access it for building the current follower set context.setIntTag(i); if (!matcher.getSubContext(context).runMatcher()) { // rule failed, so invalidate all stack actions the rule might have done context.getValueStack().restoreSnapshot(valueStackSnapshot); return false; } } context.createNode(); return true; }
public boolean match(MatcherContext context) { subMatcher.getSubContext(context).runMatcher(); context.createNode(); return true; }
public boolean match(MatcherContext<?> context) { if (inner == null && context.getMatcher().match(context) || inner != null && inner.match(context)) { if (errorIndex < context.getCurrentIndex() && notTestNot(context)) { errorIndex = context.getCurrentIndex(); } return true; } return false; }
@Override public void beforeMatch(final PreMatchEvent<V> event) { final MatcherContext<V> context = event.getContext(); final Matcher matcher = context.getMatcher(); Integer id = matcherIds.get(matcher); if (id == null) { //noinspection UnnecessaryBoxing id = Integer.valueOf(nextMatcherId); matcherIds.put(matcher, id); final MatcherType type = typeProvider.getType(matcher.getClass()); final MatcherDescriptor descriptor = new MatcherDescriptor(nextMatcherId, type, matcher); matcherDescriptors.put(matcher, descriptor); nextMatcherId++; } final int level = context.getLevel(); nodeIds.put(level, nextNodeId); nextNodeId++; prematchMatcherIds.put(level, id); final int startIndex = Math.min(nrChars, context.getCurrentIndex()); prematchIndices.put(level, startIndex); prematchTimes.put(level, System.nanoTime()); }
protected ParsingResult<V> createParsingResult(boolean matched, MatcherContext<V> rootContext) { return new ParsingResult<V>(matched, rootContext.getNode(), getValueStack(), getParseErrors(), rootContext.getInputBuffer()); } }
@SuppressWarnings({ "AutoBoxing", "AutoUnboxing" }) @Override public void matchFailure(final MatchFailureEvent<V> event) { final long endTime = System.nanoTime(); final MatcherContext<V> context = event.getContext(); final int level = context.getLevel(); final Integer parentNodeId = nodeIds.get(level - 1); final Integer nodeId = nodeIds.get(level); final int startIndex = prematchIndices.get(level); final int endIndex = context.getCurrentIndex(); final Integer matcherId = prematchMatcherIds.get(level); final long time = endTime - prematchTimes.get(level); // Write: // parent;id;level;success;matcherId;start;end;time sb.setLength(0); sb.append(parentNodeId).append(';') .append(nodeId).append(';') .append(level).append(";0;") .append(matcherId).append(';') .append(startIndex).append(';') .append(endIndex).append(';') .append(time).append('\n'); try { writer.append(sb); } catch (IOException e) { throw cleanup(e); } }
public boolean match(MatcherContext context) { context.createNode(); return true; }
public ParsingResult<V> run(InputBuffer inputBuffer) { checkArgNotNull(inputBuffer, "inputBuffer"); resetValueStack(); totalRuns++; MatcherContext<V> rootContext = createRootContext(inputBuffer, this, true); rootContext.getMatcher().accept(new DoWithMatcherVisitor(new DoWithMatcherVisitor.Action() { public void process(Matcher matcher) { RuleStats ruleStats = (RuleStats) matcher.getTag(); if (ruleStats == null) { ruleStats = new RuleStats(); matcher.setTag(ruleStats); } else { ruleStats.clear(); } } })); runMatches = 0; long timeStamp = System.nanoTime() - timeCorrection; boolean matched = rootContext.runMatcher(); totalNanoTime += System.nanoTime() - timeCorrection - timeStamp; getRootMatcher().accept(new DoWithMatcherVisitor(updateStatsAction)); return createParsingResult(matched, rootContext); }
public boolean match(MatcherContext<?> context) { return context.getMatcher().match(context); } }