/** * Throws a GrammarException if the given condition is not met. * * @param condition the condition * @param errorMessage the error message */ public static void ensure(boolean condition, String errorMessage) { if (!condition) { throw new GrammarException(errorMessage); } }
/** * Throws a GrammarException if the given condition is not met. * * @param condition the condition * @param errorMessageFormat the error message format * @param errorMessageArgs the error message arguments */ public static void ensure(boolean condition, String errorMessageFormat, Object... errorMessageArgs) { if (!condition) { throw new GrammarException(errorMessageFormat, errorMessageArgs); } }
private static void verify(char[][] strings) { int length = strings.length; for (int i = 0; i < length; i++) { char[] a = strings[i]; inner: for (int j = i + 1; j < length; j++) { char[] b = strings[j]; if (b.length < a.length) continue; for (int k = 0; k < a.length; k++) { if (a[k] != b[k]) continue inner; } String sa = '"' + String.valueOf(a) + '"'; String sb = '"' + String.valueOf(b) + '"'; String msg = a.length == b.length ? sa + " is specified twice in a FirstOf(String...)" : sa + " is a prefix of " + sb + " in a FirstOf(String...) and comes before " + sb + ", which prevents " + sb + " from ever matching! You should reverse the order of the two alternatives."; throw new GrammarException(msg); } } } }
/** * Finds the constructor of the given class that is compatible with the given arguments. * * @param type the class to find the constructor of * @param args the arguments * @return the constructor */ public static Constructor findConstructor(Class<?> type, Object[] args) { outer: for (Constructor constructor : type.getConstructors()) { Class<?>[] paramTypes = constructor.getParameterTypes(); if (paramTypes.length != args.length) continue; for (int i = 0; i < args.length; i++) { Object arg = args[i]; if (arg != null && !paramTypes[i].isAssignableFrom(arg.getClass()) && !isBoxedType(paramTypes[i], arg.getClass())) continue outer; if (arg == null && paramTypes[i].isPrimitive()) continue outer; } return constructor; } throw new GrammarException("No constructor found for %s and the given %s arguments", type, args.length); }
public char getFirstMatchChar() { checkActionContext(); int ix = subContext.startIndex; if (subContext.currentIndex <= ix) { throw new GrammarException("getFirstMatchChar called but previous rule did not match anything"); } return inputBuffer.charAt(ix); }
/** * Converts the given object to a rule. * This method can be overriden to enable the use of custom objects directly in rule specifications. * * @param obj the object to convert * @return the rule corresponding to the given object */ @DontExtend public Rule toRule(Object obj) { if (obj instanceof Rule) return (Rule) obj; if (obj instanceof Character) return fromCharLiteral((Character) obj); if (obj instanceof String) return fromStringLiteral((String) obj); if (obj instanceof char[]) return fromCharArray((char[]) obj); if (obj instanceof Action) { Action action = (Action) obj; return new ActionMatcher(action); } Checks.ensure(!(obj instanceof Boolean), "Rule specification contains an unwrapped Boolean value, " + "if you were trying to specify a parser action wrap the expression with ACTION(...)"); throw new GrammarException("'" + obj + "' cannot be automatically converted to a parser Rule"); }
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; }