CSharpPreprocessorParserBase.java 7.7 KB

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