{"record":{"id":"36380878287c3f39","repo":"stanfordnlp/CoreNLP","slug":"field-already-defined-363808","errorCode":null,"errorMessage":"Field already defined: ","messagePattern":"Field already defined: ","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ling/tokensregex/parser/TokenSequenceParser.jj","lineNumber":303,"sourceCode":"\t\t\t( (\",\" | \";\")  FieldValue(env, attributes)\n\t\t    )*\n\t\t\"}\"\n    )\n  \t{\n  \t  return new Expressions.CompositeValue(/*\"COMPOSITE\", */ attributes, false);\n\t}\n}\n\nMap<String,Expression> FieldValue(Env env, Map<String,Expression> attributes) : {\n\tString fieldname = null;\n\tExpression expr = null;\n}   {\n\t    fieldname = RelaxedString()\n\t    (    \":\" expr = Expression(env) )\n\t    {\n\t      if (fieldname != null && expr != null)  {\n\t        if (attributes.containsKey(fieldname)) {\n                throw new Error(\"Field already defined: \" + fieldname);\n\t        }\n\t        attributes.put(fieldname, expr);\n\t      }\n     \t  return attributes;\n\t    }\n\t}\n\nValue BasicValue(Env env) : {\n\tToken tok = null;\n\tToken head = null;\n\tToken tail = null;\n\tSequencePattern.PatternExpr seqRegex = null;\n}   {\n        tok = <REGEX> { return new Expressions.RegexValue(/*\"REGEX\",*/ tok.image.substring(1,tok.image.length()-1)); }\n        |\n        tok = <STR> { return new Expressions.PrimitiveValue<String>(\"STRING\", parseQuotedString(tok.image) ); }\n        |\n        tok = IntegerToken() { return new Expressions.PrimitiveValue<Number>(\"INTEGER\", parseInteger(tok.image)); }","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ling/tokensregex/parser/TokenSequenceParser.jj#L285-L321","documentation":"A plain java.lang.Error thrown inside the grammar's node-attribute production when the same field name appears twice in a node attribute map (e.g. [word:\"a\" word:\"b\"]). The parser builds a Map of attributes and rejects duplicates so match semantics stay unambiguous. Because it is a raw Error (not a ParseException), it will not be caught by catch(ParseException) blocks.","triggerScenarios":"Parsing a TokensRegex node expression that lists the same attribute name twice, e.g. [word:\"foo\" tag:\"NN\" word:\"bar\"], via parseNode or any grammar path that constructs node attributes.","commonSituations":"Hand-written rules where a copy-paste duplicated an attribute; programmatically generated node strings that concatenate attribute fragments without deduplication; merging two rule templates that share a field.","solutions":["Remove or rename the duplicate attribute in the node expression so each field appears once.","Combine duplicate fields into a single value where semantics allow (e.g. use a regex alternation for word).","If generating node strings in code, deduplicate attribute names before assembling the string.","Catch Error (or better, pre-validate) since this is thrown as java.lang.Error, not ParseException."],"exampleFix":"// before\nparser.parseNode(env, \"[word: foo tag: NN word: bar]\"); // Error: Field already defined: word\n// after\nparser.parseNode(env, \"[word: /foo|bar/ tag: NN]\");","handlingStrategy":"validation","validationCode":"// Pre-check: no repeated attribute names inside a node expression\nstatic void checkNoDupFields(String node) {\n  java.util.regex.Matcher m = java.util.regex.Pattern.compile(\"(\\\\w+)\\\\s*:\").matcher(node);\n  java.util.Set<String> seen = new java.util.HashSet<>();\n  while (m.find()) { if (!seen.add(m.group(1))) throw new IllegalArgumentException(\"Duplicate field in node: \" + m.group(1)); }\n}","typeGuard":null,"tryCatchPattern":"try {\n  parser.parseNode(env, nodeExpr);\n} catch (Error e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Field already defined\")) {\n    throw new IllegalArgumentException(\"Duplicate attribute in node: \" + nodeExpr, e);\n  }\n  throw e;\n}","preventionTips":["Deduplicate attribute names when generating node strings","Review copy-pasted rules for repeated fields","Combine alternatives into one attribute value with regex","Catch Error at rule-loading boundaries since this is not a ParseException"],"tags":["parser","tokensregex","java","duplicate-key","node-pattern"],"backgroundTag":"duplicate-key","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"}