CSharpPreprocessorParserBase.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package de.jplag.csharp.grammar;
  2. import java.util.HashSet;
  3. import java.util.Stack;
  4. import org.antlr.v4.runtime.*;
  5. /**
  6. * This class was taken from <a href="https://github.com/antlr/grammars-v4/tree/master/csharp">antlr/grammars-v4</a>. It
  7. * was originally written by Ken Domino. Note that this class is licensed under Eclipse Public License - v 1.0.
  8. */
  9. abstract class CSharpPreprocessorParserBase extends Parser {
  10. protected CSharpPreprocessorParserBase(TokenStream input) {
  11. super(input);
  12. conditions.push(true);
  13. ConditionalSymbols.add("DEBUG");
  14. }
  15. Stack<Boolean> conditions = new Stack<Boolean>();
  16. public HashSet<String> ConditionalSymbols = new HashSet<String>();
  17. protected Boolean AllConditions() {
  18. for (Boolean condition : conditions) {
  19. if (!condition)
  20. return false;
  21. }
  22. return true;
  23. }
  24. protected void OnPreprocessorDirectiveDefine() {
  25. ParserRuleContext c = this._ctx;
  26. CSharpPreprocessorParser.PreprocessorDeclarationContext d = (CSharpPreprocessorParser.PreprocessorDeclarationContext) c;
  27. ConditionalSymbols.add(d.CONDITIONAL_SYMBOL().getText());
  28. d.value = AllConditions();
  29. }
  30. protected void OnPreprocessorDirectiveUndef() {
  31. ParserRuleContext c = this._ctx;
  32. CSharpPreprocessorParser.PreprocessorDeclarationContext d = (CSharpPreprocessorParser.PreprocessorDeclarationContext) c;
  33. ConditionalSymbols.remove(d.CONDITIONAL_SYMBOL().getText());
  34. d.value = AllConditions();
  35. }
  36. protected void OnPreprocessorDirectiveIf() {
  37. ParserRuleContext c = this._ctx;
  38. CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
  39. d.value = d.expr.value.equals("true") && AllConditions();
  40. conditions.push(d.expr.value.equals("true"));
  41. }
  42. protected void OnPreprocessorDirectiveElif() {
  43. ParserRuleContext c = this._ctx;
  44. CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
  45. if (!conditions.peek()) {
  46. conditions.pop();
  47. d.value = d.expr.value.equals("true") && AllConditions();
  48. conditions.push(d.expr.value.equals("true"));
  49. } else {
  50. d.value = false;
  51. }
  52. }
  53. protected void OnPreprocessorDirectiveElse() {
  54. ParserRuleContext c = this._ctx;
  55. CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
  56. if (!conditions.peek()) {
  57. conditions.pop();
  58. d.value = true && AllConditions();
  59. conditions.push(true);
  60. } else {
  61. d.value = false;
  62. }
  63. }
  64. protected void OnPreprocessorDirectiveEndif() {
  65. ParserRuleContext c = this._ctx;
  66. CSharpPreprocessorParser.PreprocessorConditionalContext d = (CSharpPreprocessorParser.PreprocessorConditionalContext) c;
  67. conditions.pop();
  68. d.value = conditions.peek();
  69. }
  70. protected void OnPreprocessorDirectiveLine() {
  71. ParserRuleContext c = this._ctx;
  72. CSharpPreprocessorParser.PreprocessorLineContext d = (CSharpPreprocessorParser.PreprocessorLineContext) c;
  73. d.value = AllConditions();
  74. }
  75. protected void OnPreprocessorDirectiveError() {
  76. ParserRuleContext c = this._ctx;
  77. CSharpPreprocessorParser.PreprocessorDiagnosticContext d = (CSharpPreprocessorParser.PreprocessorDiagnosticContext) c;
  78. d.value = AllConditions();
  79. }
  80. protected void OnPreprocessorDirectiveWarning() {
  81. ParserRuleContext c = this._ctx;
  82. CSharpPreprocessorParser.PreprocessorDiagnosticContext d = (CSharpPreprocessorParser.PreprocessorDiagnosticContext) c;
  83. d.value = AllConditions();
  84. }
  85. protected void OnPreprocessorDirectiveRegion() {
  86. ParserRuleContext c = this._ctx;
  87. CSharpPreprocessorParser.PreprocessorRegionContext d = (CSharpPreprocessorParser.PreprocessorRegionContext) c;
  88. d.value = AllConditions();
  89. }
  90. protected void OnPreprocessorDirectiveEndregion() {
  91. ParserRuleContext c = this._ctx;
  92. CSharpPreprocessorParser.PreprocessorRegionContext d = (CSharpPreprocessorParser.PreprocessorRegionContext) c;
  93. d.value = AllConditions();
  94. }
  95. protected void OnPreprocessorDirectivePragma() {
  96. ParserRuleContext c = this._ctx;
  97. CSharpPreprocessorParser.PreprocessorPragmaContext d = (CSharpPreprocessorParser.PreprocessorPragmaContext) c;
  98. d.value = AllConditions();
  99. }
  100. protected void OnPreprocessorDirectiveNullable() {
  101. ParserRuleContext c = this._ctx;
  102. CSharpPreprocessorParser.PreprocessorNullableContext d = (CSharpPreprocessorParser.PreprocessorNullableContext) c;
  103. d.value = AllConditions();
  104. }
  105. protected void OnPreprocessorExpressionTrue() {
  106. ParserRuleContext c = this._ctx;
  107. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  108. d.value = "true";
  109. }
  110. protected void OnPreprocessorExpressionFalse() {
  111. ParserRuleContext c = this._ctx;
  112. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  113. d.value = "false";
  114. }
  115. protected void OnPreprocessorExpressionConditionalSymbol() {
  116. ParserRuleContext c = this._ctx;
  117. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  118. d.value = ConditionalSymbols.contains(d.CONDITIONAL_SYMBOL().getText()) ? "true" : "false";
  119. }
  120. protected void OnPreprocessorExpressionConditionalOpenParens() {
  121. ParserRuleContext c = this._ctx;
  122. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  123. d.value = d.expr.value;
  124. }
  125. protected void OnPreprocessorExpressionConditionalBang() {
  126. ParserRuleContext c = this._ctx;
  127. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  128. d.value = d.expr.value.equals("true") ? "false" : "true";
  129. }
  130. protected void OnPreprocessorExpressionConditionalEq() {
  131. ParserRuleContext c = this._ctx;
  132. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  133. d.value = (d.expr1.value == d.expr2.value ? "true" : "false");
  134. }
  135. protected void OnPreprocessorExpressionConditionalNe() {
  136. ParserRuleContext c = this._ctx;
  137. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  138. d.value = (d.expr1.value != d.expr2.value ? "true" : "false");
  139. }
  140. protected void OnPreprocessorExpressionConditionalAnd() {
  141. ParserRuleContext c = this._ctx;
  142. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  143. d.value = (d.expr1.value.equals("true") && d.expr2.value.equals("true") ? "true" : "false");
  144. }
  145. protected void OnPreprocessorExpressionConditionalOr() {
  146. ParserRuleContext c = this._ctx;
  147. CSharpPreprocessorParser.Preprocessor_expressionContext d = (CSharpPreprocessorParser.Preprocessor_expressionContext) c;
  148. d.value = (d.expr1.value.equals("true") || d.expr2.value.equals("true") ? "true" : "false");
  149. }
  150. }