I am writing a recursive descent parser.
Let ParseContext
specifies the context for parsing. Pseudocode:
class ParseContext {
Logger logger; // logger for error/warning messages
Locale locale; // locale for error/warning messages
}
Classes derived from ParseContext
may specify additional parse options, such as the version of the grammar or whether warnings should be considered as errors.
Now define Parser
.
1.
class Parser {
Parser(ParseContext context) {
this.context = context;
}
abstract function parse(String text) return Object;
}
or
2.
class Parser {
Parser() {
}
abstract function parse(ParseContext context, String text) return Object;
}
Which of the above Parser
classes is a know pattern and which is a know antipattern? Or maybe, neither is in the set of known antipatterns? The first seems to be an instance of dependency injection pattern. Does it imply that the other is an antipattern?
It seems that it is unusual to call .parse
with several different context
arguments. Does it imply that it makes no sense to refactor "2" to be a constructor option instead (as in "1")?
What are (dis)advantages of the either?