[il-antlr-interest: 31369] [antlr-interest] Partial grammar for class counting

2011-02-06 Thread Pavel Martynov
Hi!

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 Liststring _classCollector = new Liststring();
        public Liststring 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; }
        ;



Hi!

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 Liststring _classCollector = new Liststring();
        public Liststring 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!


--
with best regards, Pavel Martynov

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
il-antlr-interest group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 31370] Re: [antlr-interest] Partial grammar for class counting

2011-02-06 Thread Massimiliano Donini

  Hi pavel,
Did you try to use Lexical Filter?
Have a look http://www.antlr.org/wiki/display/ANTLR3/Lexical+filters

Hope it helps
Best Regards Max


Il 06/02/2011 17:04, Pavel Martynov ha scritto:
 Hi!

 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 Liststring  _classCollector = new Liststring();
  public Liststring  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; }
  ;



 Hi!

 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 Liststring  _classCollector = new Liststring();
  public Liststring  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!


 --
 with best regards, Pavel Martynov

 List: http://www.antlr.org/mailman/listinfo/antlr-interest
 Unsubscribe: 
 http://www.antlr.org/mailman/options/antlr-interest/your-email-address




List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
il-antlr-interest group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.



[il-antlr-interest: 31371] Re: [antlr-interest] Partial grammar for class counting

2011-02-06 Thread Bart Kiers

 Il 06/02/2011 17:04, Pavel Martynov ha scritto:
  Hi!
  
  with best regards, Pavel Martynov
 


Answered here:
http://stackoverflow.com/questions/4914073/partial-grammar-for-counting-class-count/4914224#4914224
as
well.

http://stackoverflow.com/questions/4914073/partial-grammar-for-counting-class-count/4914224#4914224
HTH,

Bart.

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
il-antlr-interest group.
To post to this group, send email to il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.