计数类计数的部分语法 [英] Partial grammar for counting class count
问题描述
我需要计算在正确的C#源文件中的类数。
我写了以下语法:
I need to count the number of classes in correct C# source file. I wrote the following grammar:
grammar CSharpClassGrammar;
options
{
language=CSharp2;
}
@parser::namespace { CSharpClassGrammar.Generated }
@lexer::namespace { CSharpClassGrammar.Generated }
@header
{
using System;
using System.Collections.Generic;
}
@members
{
private List<string> _classCollector = new List<string>();
public List<string> ClassCollector { get { return
_classCollector; } }
}
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
csfile : class_declaration* EOF
;
class_declaration
: (ACCESSLEVEL | MODIFIERS)* PARTIAL? 'class' CLASSNAME
class_body
';'?
{ _classCollector.Add($CLASSNAME.text); }
;
class_body
: '{' class_declaration* '}'
;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
ACCESSLEVEL
: 'public' | 'internal' | 'protected' | 'private' | 'protected
internal'
;
MODIFIERS
: 'static' | 'sealed' | 'abstract'
;
PARTIAL
: 'partial'
;
CLASSNAME
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
COMMENT
: '//' ~('\n'|'\r')* {$channel=HIDDEN;}
| '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
;
WHITESPACE
: ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; }
;
此解析器正确计算空类(以及嵌套类)与空类实体:
This parser correctly count empty classes (and nested classes too) with empty class-body:
internal class DeclarationClass1
{
class DeclarationClass2
{
public class DeclarationClass3
{
abstract class DeclarationClass4
{
}
}
}
}
我需要计算非空的类,例如:
I need to count classes with not empty body, such as:
class TestClass
{
int a = 42;
class Nested { }
}
忽略所有的代码是不是类声明。
在上面的例子中,忽略
I need to somehow ignore all the code that is "not a class declaration". In the example above ignore
int a = 42;
我该如何做?可能是其他语言的示例吗?
请帮忙!
How can I do this? May be example for other language?
Please, help!
推荐答案
当您只对某个源文件的某些部分感兴趣时,可以设置 filter = true 部分中的选项{...} 这将使您只定义那些您感兴趣的令牌,以及您未定义的令牌,被词法分析器忽略。
When you're only interested in certain parts of a source file, you could set filter=true
in your options { ... } sections. This will enable you to only define those tokens you're interested in, and what you don't define, is ignored by the lexer.
请注意,这仅适用于
一个小演示:
lexer grammar CSharpClassLexer;
options {
language=CSharp2;
filter=true;
}
@namespace { Demo }
Comment
: '//' ~('\r' | '\n')*
| '/*' .* '*/'
;
String
: '"' ('\\' . | ~('"' | '\\' | '\r' | '\n'))* '"'
| '@' '"' ('"' '"' | ~'"')* '"'
;
Class
: 'class' Space+ Identifier
{Console.WriteLine("Found class: " + $Identifier.text);}
;
Space
: ' ' | '\t' | '\r' | '\n'
;
Identifier
: ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '_' | '0'..'9')*
;
请务必将标识符
因为你不想将 Xclass Foo
标记为: ['X','class','Foo']
。在标识符
中, Xclass
将成为整个标识符。
It's important you leave the Identifier
in there because you don't want Xclass Foo
to be tokenized as: ['X', 'class', 'Foo']
. With the Identifier
in there, Xclass
will become the entire identifier.
语法可以用以下类测试:
The grammar can be tested with the following class:
using System;
using Antlr.Runtime;
namespace Demo
{
class MainClass
{
public static void Main (string[] args)
{
string source =
@"class TestClass
{
int a = 42;
string _class = ""inside a string literal: class FooBar {}..."";
class Nested {
/* class NotAClass {} */
// class X { }
class DoubleNested {
string str = @""
multi line string
class Bar {}
"";
}
}
}";
Console.WriteLine("source=\n" + source + "\n-------------------------");
ANTLRStringStream Input = new ANTLRStringStream(source);
CSharpClassLexer Lexer = new CSharpClassLexer(Input);
CommonTokenStream Tokens = new CommonTokenStream(Lexer);
Tokens.GetTokens();
}
}
}
source=
class TestClass
{
int a = 42;
string _class = "inside a string literal: class FooBar {}...";
class Nested {
/* class NotAClass {} */
// class X { }
class DoubleNested {
string str = @"
multi line string
class Bar {}
";
}
}
}
-------------------------
Found class: TestClass
Found class: Nested
Found class: DoubleNested
只是一个快速演示,我不知道如果我在语法中处理正确的字符串文字(我不熟悉C#),但这个演示应该给你一个开始。
Note that this is just a quick demo, I am not sure if I handled the proper string literals in the grammar (I am unfamiliar with C#), but this demo should give you a start.
祝你好运!
这篇关于计数类计数的部分语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!