File tree 2 files changed +38
-2
lines changed
main/kotlin/chapi/ast/csharpast
test/kotlin/chapi/ast/csharpast
2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -56,9 +56,24 @@ open class CSharpAnalyser : Analyser {
56
56
preprocessorParser.inputStream = directiveTokenStream
57
57
preprocessorParser.reset()
58
58
// Parse condition in preprocessor directive (based on CSharpPreprocessorParser.g4 grammar).
59
- val directive: CSharpPreprocessorParser .Preprocessor_directiveContext = preprocessorParser.preprocessor_directive()
59
+ val directive = preprocessorParser.preprocessor_directive()
60
+
60
61
// if true than next code is valid and not ignored.
61
62
compiledTokens = directive.value
63
+ val directiveStr = tokens[index + 1 ].text.trim { it <= ' ' }
64
+ if (" line" == directiveStr || " error" == directiveStr || " warning" == directiveStr || " define" == directiveStr || " endregion" == directiveStr || " endif" == directiveStr || " pragma" == directiveStr) {
65
+ compiledTokens = true
66
+ }
67
+ var conditionalSymbol: String?
68
+ if (" define" == tokens[index + 1 ].text) {
69
+ // add to the conditional symbols
70
+ conditionalSymbol = tokens[index + 2 ].text
71
+ preprocessorParser.ConditionalSymbols .add(conditionalSymbol)
72
+ }
73
+ if (" undef" == tokens[index + 1 ].text) {
74
+ conditionalSymbol = tokens[index + 2 ].text
75
+ preprocessorParser.ConditionalSymbols .remove(conditionalSymbol)
76
+ }
62
77
index = directiveTokenIndex - 1
63
78
}
64
79
token.channel == CSharpLexer .COMMENTS_CHANNEL -> {
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ package chapi.ast.csharpast
3
3
import org.junit.jupiter.api.Test
4
4
import kotlin.test.assertEquals
5
5
6
- internal class CSharpAstListenerTest {
6
+ internal class CSharpAstListenerTest {
7
7
private val helloworld = """
8
8
using System;
9
9
@@ -336,5 +336,26 @@ namespace Chapi {
336
336
val structs = codeContainer.Containers [0 ].DataStructures
337
337
assertEquals(structs.size, 1 )
338
338
}
339
+
340
+ @Test
341
+ fun macroPreprocessor () {
342
+ val code = """
343
+ using System.Text;
344
+ namespace testns {
345
+ public class testcls {
346
+ public static void Main(string []args) {
347
+ #if DEBUG
348
+ int x = 2;
349
+ #else
350
+ int y = 10;
351
+ #endif
352
+ }
353
+ }
354
+ }"""
355
+ val codeContainer = CSharpAnalyser ().analysis(code, " test.cs" )
356
+ val structs = codeContainer.Containers [0 ].DataStructures
357
+ println (structs)
358
+ assertEquals(structs.size, 1 )
359
+ }
339
360
}
340
361
You can’t perform that action at this time.
0 commit comments