{"record":{"id":"5a37cd9c816148bf","repo":"stanfordnlp/CoreNLP","slug":"error-when-parsing","errorCode":null,"errorMessage":"Error when parsing ","messagePattern":"Error when parsing ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/TokenSequencePattern.java","lineNumber":191,"sourceCode":"  }\n\n  /**\n   * Compiles a regular expression over tokens into a TokenSequencePattern\n   * using the specified environment.\n   *\n   * @param env Environment to use\n   * @param string Regular expression to be compiled\n   * @return Compiled TokenSequencePattern\n   */\n  public static TokenSequencePattern compile(Env env, String string) {\n    try {\n//      SequencePattern.PatternExpr nodeSequencePattern = TokenSequenceParser.parseSequence(env, string);\n//      return new TokenSequencePattern(string, nodeSequencePattern);\n      // TODO: Check token sequence parser?\n      Pair<PatternExpr, SequenceMatchAction<CoreMap>> p = env.parser.parseSequenceWithAction(env, string);\n      return new TokenSequencePattern(string, p.first(), p.second());\n    } catch (Exception ex) {\n      throw new RuntimeException(\"Error when parsing \" + string, ex);\n    }\n  }\n\n  /**\n   * Compiles a sequence of regular expressions into a TokenSequencePattern\n   * using the default environment.\n   *\n   * @param strings List of regular expression to be compiled\n   * @return Compiled TokenSequencePattern\n   */\n  public static TokenSequencePattern compile(String... strings)\n  {\n    return compile(DEFAULT_ENV, strings);\n  }\n\n  /**\n   * Compiles a sequence of regular expressions into a TokenSequencePattern\n   * using the specified environment.","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/TokenSequencePattern.java#L173-L209","documentation":"TokenSequencePattern.compile wraps any exception from the token-sequence parser in RuntimeException('Error when parsing ' + string). It means the pattern string is not syntactically valid TokensRegex (or the environment/parser failed while compiling it); the original cause is attached as the exception cause.","triggerScenarios":"TokenSequencePattern.compile(env, string) or compile(string) with a malformed pattern such as unbalanced brackets, illegal tokens, bad quantifier syntax, or references to undefined rules/variables in the Env.","commonSituations":"Hand-written TokensRegex rules in config files with typos; patterns copied from regex that use unsupported syntax; missing environment bindings after upgrading CoreNLP models/pipelines.","solutions":["Read the getCause() of the RuntimeException — it contains the actual parser error and position.","Validate brackets, quotes, and case/quantifier syntax in the pattern string.","Ensure all variables/labels used by the pattern are registered in the Env before compile.","Test the pattern in isolation with a minimal compile call to isolate the failing fragment.","For TokenMgrError-style lexer failures, check for illegal characters in the string."],"exampleFix":"// before\nTokenSequencePattern p = TokenSequencePattern.compile(\"[ { word:/NN/ } \"); // unbalanced\n// after\nTokenSequencePattern p = TokenSequencePattern.compile(\"[ { word:/NN/ } ]\");","handlingStrategy":"try-catch","validationCode":"if (pattern == null || pattern.trim().isEmpty()) throw new IllegalArgumentException(\"empty TokensRegex pattern\");\nint depth = 0;\nfor (char c : pattern.toCharArray()) {\n    if (c == '[') depth++;\n    if (c == ']') depth--;\n    if (depth < 0) throw new IllegalArgumentException(\"unbalanced brackets in: \" + pattern);\n}\nif (depth != 0) throw new IllegalArgumentException(\"unbalanced brackets in: \" + pattern);","typeGuard":null,"tryCatchPattern":"try {\n    TokenSequencePattern p = TokenSequencePattern.compile(env, pattern);\n} catch (RuntimeException e) {\n    // 'Error when parsing <string>' — inspect e.getCause() for the real parser error\n    throw new InvalidPatternException(\"bad pattern: \" + pattern, e.getCause());\n}","preventionTips":["Always log e.getCause(), not just the wrapper message.","Validate pattern strings (balanced brackets/quotes) before compile.","Keep patterns in reviewed resource files rather than inline string concatenation.","Pin CoreNLP versions and re-test all rule patterns on upgrades."],"tags":["java","tokensregex","pattern-parsing","runtime-exception"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}