| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package de.jplag.csharp.grammar;
- import java.util.HashSet;
- import java.util.Stack;
- import org.antlr.v4.runtime.*;
- /**
- * This class was taken from <a href="https://github.com/antlr/grammars-v4/tree/master/csharp">antlr/grammars-v4</a>. It
- * was originally written by Ken Domino. Note that this class is licensed under Eclipse Public License - v 1.0.
- */
- abstract class CSharpPreprocessorParserBase extends Parser {
- protected CSharpPreprocessorParserBase(TokenStream input) {
- super(input);
- conditions.push(true);
- ConditionalSymbols.add("DEBUG");
- }
- Stack<Boolean> conditions = new Stack<Boolean>();
- public HashSet<String> ConditionalSymbols = new HashSet<String>();
- protected Boolean AllConditions() {
- for (Boolean condition : conditions) {
- if (!condition)
- return false;
- }
- return true;
- }
- protected void OnPreprocessorDirectiveDefine() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorDeclarationContext d = (CSharpPreprocessorParser.PreprocessorDeclarationContext) c;
- ConditionalSymbols.add(d.CONDITIONAL_SYMBOL().getText());
- d.value = AllConditions();
- }
- protected void OnPreprocessorDirectiveUndef() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorDeclarationContext d = (CSharpPreprocessorParser.PreprocessorDeclarationContext) c;
- ConditionalSymbols.remove(d.CONDITIONAL_SYMBOL().getText());
- d.value = AllConditions();
- }
- protected void OnPreprocessorDirectiveIf() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
- d.value = d.expr.value.equals("true") && AllConditions();
- conditions.push(d.expr.value.equals("true"));
- }
- protected void OnPreprocessorDirectiveElif() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
- if (!conditions.peek()) {
- conditions.pop();
- d.value = d.expr.value.equals("true") && AllConditions();
- conditions.push(d.expr.value.equals("true"));
- } else {
- d.value = false;
- }
- }
- protected void OnPreprocessorDirectiveElse() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
- if (!conditions.peek()) {
- conditions.pop();
- d.value = true && AllConditions();
- conditions.push(true);
- } else {
- d.value = false;
- }
- }
- protected void OnPreprocessorDirectiveEndif() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
- conditions.pop();
- d.value = conditions.peek();
- }
- protected void OnPreprocessorDirectiveLine() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorLineContext d = (CSharpPreprocessorParser.PreprocessorLineContext) c;
- d.value = AllConditions();
- }
- protected void OnPreprocessorDirectiveError() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorDiagnosticContext d = (CSharpPreprocessorParser.PreprocessorDiagnosticContext) c;
- d.value = AllConditions();
- }
- protected void OnPreprocessorDirectiveWarning() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorDiagnosticContext d = (CSharpPreprocessorParser.PreprocessorDiagnosticContext) c;
- d.value = AllConditions();
- }
- protected void OnPreprocessorDirectiveRegion() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorRegionContext d = (CSharpPreprocessorParser.PreprocessorRegionContext) c;
- d.value = AllConditions();
- }
- protected void OnPreprocessorDirectiveEndregion() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorRegionContext d = (CSharpPreprocessorParser.PreprocessorRegionContext) c;
- d.value = AllConditions();
- }
- protected void OnPreprocessorDirectivePragma() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorPragmaContext d = (CSharpPreprocessorParser.PreprocessorPragmaContext) c;
- d.value = AllConditions();
- }
- protected void OnPreprocessorDirectiveNullable() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.PreprocessorNullableContext d = (CSharpPreprocessorParser.PreprocessorNullableContext) c;
- d.value = AllConditions();
- }
- protected void OnPreprocessorExpressionTrue() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = "true";
- }
- protected void OnPreprocessorExpressionFalse() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = "false";
- }
- protected void OnPreprocessorExpressionConditionalSymbol() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = ConditionalSymbols.contains(d.CONDITIONAL_SYMBOL().getText()) ? "true" : "false";
- }
- protected void OnPreprocessorExpressionConditionalOpenParens() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = d.expr.value;
- }
- protected void OnPreprocessorExpressionConditionalBang() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = d.expr.value.equals("true") ? "false" : "true";
- }
- protected void OnPreprocessorExpressionConditionalEq() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = (d.expr1.value == d.expr2.value ? "true" : "false");
- }
- protected void OnPreprocessorExpressionConditionalNe() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = (d.expr1.value != d.expr2.value ? "true" : "false");
- }
- protected void OnPreprocessorExpressionConditionalAnd() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = (d.expr1.value.equals("true") && d.expr2.value.equals("true") ? "true" : "false");
- }
- protected void OnPreprocessorExpressionConditionalOr() {
- ParserRuleContext c = this._ctx;
- CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
- d.value = (d.expr1.value.equals("true") || d.expr2.value.equals("true") ? "true" : "false");
- }
- }
|