All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javacc-7.0.3.grammars.AsnParser.jj Maven / Gradle / Ivy

There is a newer version: 7.0.13
Show newest version
/*
 *
 *  ASN.1 grammar  for JavaCC
 *
 *  Contributed by Helena Sarin ([email protected])
 *
 *  Derived in part from the following work: snacc - a freeware ASN.1 to C or C++ compiler, v 1.3,
 *  yacc/lex source code ( parse-asn1.y, lex-asn1.l), 
 *  the free software, which is covered by GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or later
 *
 *  I tested it against several ASN.1 files used by CMIP;
 *  Additional work is reguired to fully support SNMP MIBs parsing
 *   
 *  Please let me know if you use this grammar: i'm using it to develop ASN.1/IDL compiler
 *  
 */

options {
SANITY_CHECK=true;
DEBUG_PARSER=false;
DEBUG_TOKEN_MANAGER=false;
}

PARSER_BEGIN(AsnParser)

import java.util.*;

public class AsnParser {


  public static void main(String args[]) throws ParseException {

    AsnParser parser;	
    parser = new AsnParser(System.in);
    try {
        parser.Input();	
        System.out.println("ASN.1 file parsed successfully.");
      }
      catch(ParseException e){
        System.out.println(e.toString());
      }
  }

  private static String usefulTypes[] = { 
        "GraphicString",  "NumericString", "PrintableString",
	"TeletexString", "T61String", "VideotexString", "IA5String",
	"VisibleString", "ISO646String", "GeneralizedTime", "UniversalString", "BMPString" };
}

PARSER_END(AsnParser)

SKIP :
{
  " "
| "\t"
| "\n"
| "\r"
}

SPECIAL_TOKEN :
{
  "--" : WithinASNComment
}

 SPECIAL_TOKEN :
{
  <("--" | "\n" | "\r" | "\r\n" )> : DEFAULT
}

 MORE :
{
  <~[]>
}

TOKEN : 
{
 
}

TOKEN : 
{
 
}

TOKEN : 
{
 
}

TOKEN : 
{
 
}

TOKEN : 
{
 
}

TOKEN : 
{
 
}

TOKEN :
{
  
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
| 
|  
|  
}

TOKEN : 
{
 
}


TOKEN : 
{
 
}

void Input() : {} 
{ 
 (ModuleDefinition())*
}

void ModuleDefinition() : {} 
{ 
  ModuleIdentifier()  [TagDefault()]   ModuleBody()  
}

void TagDefault() : {} 
{ 	
   ( |   )  
}

void ModuleIdentifier() : {  } 
{ 
   modulereference() [AssignedIdentifier()]
}

void AssignedIdentifier() : {} 
{ 
   ObjectIdentifierValue() 
}

void ModuleBody() : {} 
{ 
  [Exports()] [Imports()] [AssignmentList()] 
}

void Exports() : {} 
{ 
   [SymbolsExported()]  
}

void SymbolsExported() : {} 
{ 
   ExportSymbolList() 
}

void ExportSymbolList() : {} 
{ 
   Symbol() ( Symbol())*
}

void Imports() : {} 
{ 
    [SymbolsImported()]  
}

void SymbolsImported() : {} 
{ 
   SymbolsFromModuleList() 
}

void SymbolsFromModuleList() : {} 
{ 
   SymbolsFromModule() (SymbolsFromModule())*
}

void SymbolsFromModule() : { String module; } 
{ 
   SymbolList()   ModuleIdentifier() 
}

void SymbolList() : {} 
{ 
   Symbol() ( Symbol())* 
}

void Symbol() : {} 
{ 
   typereference()
 | identifier() 
 | DefinedMacroName() 
}

void AssignmentList() : {} 
{ 
  Assignment() [] ( Assignment() [] )*
}

void Assignment() : {} 
{ 
   LOOKAHEAD(2)  MacroDefinition() |  TypeAssignment() |  ValueAssignment()
}

void MacroDefinition() : { } 
{
   ( |  | typereference())   
	  MacroBody() 
}

JAVACODE
void MacroBody() {	
  Token tok;
  int nesting = 1;
  while (true) {
    tok = getToken(1);
    if (tok.kind == END_TKN) {
	break;	
    }
    tok = getNextToken();
  }
}

void MacroReference() : {} 
{ 
   typereference() | DefinedMacroName() 
}

void TypeAssignment() : { }
{
   typereference()     GlobalType()
}

void GlobalType() : { }
{
  Type() |  DefinedMacroType() 
}

void Type() : { }
{ 
   (LOOKAHEAD(2) BuiltinType() |  DefinedType())  [LOOKAHEAD(2) SubtypeSpec()] 
}

void DefinedType() : { } 
{  
  [LOOKAHEAD(2) modulereference() ]  typereference() 
}

void BuiltinType() : {} 
{ 
  IntegerType() 
| BitStringType() 
| LOOKAHEAD(2) SetOrSequenceType()
| SetOrSequenceOfType()
| ChoiceType() 
| SelectionType() 
| TaggedType() 
| AnyType() 
| EnumeratedType() 
|   
| 
| 
| 
|  
|   
}

void EnumeratedType() : {} 
{ 
     NamedNumberList()  
}

void IntegerType() : {} 
{ 
  [  NamedNumberList()  ]
}

void BitStringType() : {} 
{ 
    [  NamedNumberList()   ] 
}

void NamedNumberList() : {  } 
{ 
  NamedNumber() ( NamedNumber())*
}

void NamedNumber() : { } 
{ 
  identifier()  ( SignedNumber() | DefinedValue()) 
                             
}

void SignedNumber() : { } 
{ 
  [  ] (  |  ) 
}

void SetOrSequenceOfType() : { }
{ 
   (  |  ) [[] SizeConstraint() []]  
    Type()
}

void SetOrSequenceType() : { } 
{ 
  ( | )  [ ElementTypeList() ] 
}

void ChoiceType() : { } 
{ 
     ElementTypeList()  
}

void ElementTypeList() : {} 
{ 
   ElementType() ( ElementType())*
}

void ElementType() : {} 
{ 
  ( NamedType()  [     (    ) | (  NamedValue() ) ] )
| ComponentsType()
}

void ComponentsType() : {  }
{
     Type() 

}

void NamedType() : { } 
{ 
  [LOOKAHEAD(2) identifier()]  Type() 
}

void SelectionType() : {} 
{ 
   identifier()  Type() 
}

void TaggedType() : { } 
{ 
  Tag() [  |  ] Type() 
}

void Tag() : {} 
{ 
    [Class()] ClassNumber()  
}

void ClassNumber() : {} 
{ 
   number() | DefinedValue() 
}

void Class() : {} 
{ 
    |  |  
}

void AnyType() : {} 
{ 
   
  [   identifier()   ]
}

void SubtypeSpec() : {} 
{ 
    SubtypeValueSetList()  
}

void SubtypeValueSetList() : {} 
{ 
   SubtypeValueSet() ( SubtypeValueSet())* 
}

void SubtypeValueSet() : {} 
{ 
 (
  ValueRange() | ContainedSubtype() | PermittedAlphabet() | SizeConstraint() | InnerTypeConstraints() )
}


void ContainedSubtype() : {} 
{ 
    Type() 
}

void SingleValue() : {} 
{ 
   Value() 
}

void ValueRange() : {} 
{ 
 ( Value() |  ) [ [  ]    [  ] (Value() |  ) ]
}

void SizeConstraint() : {} 
{ 
    SubtypeSpec() 
}

void PermittedAlphabet() : {} 
{ 
    SubtypeSpec() 
}

void InnerTypeConstraints() : {} 
{ 
   (( SingleTypeConstraint() ) | (   MultipleTypeConstraints()))
}

void SingleTypeConstraint() : {} 
{ 
   SubtypeSpec() 
}

void MultipleTypeConstraints() : {} 
{ 
   [     ] TypeConstraints()  
}

void TypeConstraints() : {} 
{ 
   NamedConstraint() ( NamedConstraint())*
}

void NamedConstraint() : {} 
{ 
    [identifier() ] Constraint() 
}

void Constraint() : {} 
{ 
  [ValueConstraint()] [PresenceConstraint()] 
}

void ValueConstraint() : {} 
{ 
   SubtypeSpec() 
}

void PresenceConstraint() : {} 
{ 
    |  |  
}



void ValueAssignment() : { } 
{     
    identifier() 
    GlobalType()  [ LOOKAHEAD(2) identifier() [] ] [  BuiltinValue() ]
   
}

void Value() : {} 
{ 
   LOOKAHEAD(2) BuiltinValue() | DefinedValue() 
}

void DefinedValue() : {} 
{ 
 [ modulereference()  ] identifier()
}

void BuiltinValue() : {} 
{ 
   BooleanValue() 
| NullValue() 
| SpecialRealValue() 
| SignedNumber() 
| HexString() 
| BinaryString() 
| CharString() 
| CompoundValue()
}

void CompoundValue() : {}
{
    ( ObjIdComponentList() | skip_to_matching_brace() )  
}

JAVACODE
void skip_to_matching_brace() {
  Token tok;
  int nesting = 1;
  while (true) {
    tok = getToken(1);
    if (tok.kind == LEFTBRACE_TKN) nesting++;
    if (tok.kind == RIGHTBRACE_TKN) {
      nesting--;
         if (nesting == 0) break;
    }
    tok = getNextToken();
  }
}

void BooleanValue() : {} 
{ 
    |  
}

void SpecialRealValue() : {} 
{ 
    |  
}

void NullValue() : {} 
{ 
    
}

void NamedValue() : {} 
{ 
  [ LOOKAHEAD(2) identifier() ] Value() 
}

void ObjectIdentifierValue() : {} 
{ 
    ObjIdComponentList()  
}

void ObjIdComponentList() : {} 
{ 
   ObjIdComponent() (ObjIdComponent())*
}

void ObjIdComponent() : {} 
{ 
  ( NumberForm() |   NameAndNumberForm())
}

void NumberForm() : {} 
{ 
   number() 
}


void NameAndNumberForm() : {} 
{ 
  identifier() [ (NumberForm() | DefinedValue()) ] 
}

void BinaryString() : {} 
{ 
    
}

void HexString() : {} 
{ 
    
}

void CharString() : {} 
{ 
    
}

void number() : { } 
{ 
    | 
}

void identifier() : {} 
{ 
    
}

void modulereference() : {} 
{ 
    
}

void typereference() : {} 
{ 
   
}

void DefinedMacroType() : {} 
{ 
  TextualConventionMacroType() 
| SnmpObjectTypeMacroType()
}

void DefinedMacroName() : {} 
{ 
   
| 
}

void SnmpObjectTypeMacroType() : {} 
{ 
     Type()  SnmpAccess()  SnmpStatus() [SnmpDescrPart()] [SnmpReferPart()] 
       [SnmpIndexPart()] [SnmpDefValPart()] 
}

void SnmpAccess() : {} 
{ 
   identifier() 
}

void SnmpStatus() : {} 
{ 
  identifier() 
}

void SnmpDescrPart() : {} 
{ 
    
}

void SnmpReferPart() : {} 
{ 
    
}

void SnmpIndexPart() : {} 
{ 
     TypeOrValueList()  
}

void TypeOrValueList() : {} 
{ 
  TypeOrValue() ( TypeOrValue())*
}

void TypeOrValue() : {} 
{ 
  LOOKAHEAD(3) Type() | Value()
}

void SnmpDefValPart() : {} 
{ 
     Value()  
}

void TextualConventionMacroType() : {}
{
    [DisplayHint()]  SnmpStatus() [SnmpDescrPart()] [SnmpReferPart()]  Type()
}

void DisplayHint() : {}
{
   
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy