📄 mib.parser
字号:
%parser
%namespace RFC1157
%start Mib
%symbol ObjectType {
public string help = "";
public ObjectType(Attribs b) { help = b.help; }
}
%symbol Defs {
public static System.Collections.Hashtable defs = new System.Collections.Hashtable(); // string->Def
public static void Enter(Def d) {
Def a = (Def)defs[d.name];
if (a==null) {
defs[d.name] = d;
if (d.name=="iso")
d.ix = 1;
} else {
bool merge = true;
if (a.parentname.Length>0 && d.parentname.Length>0 &&
a.parentname!=d.parentname)
merge = false;
if (a.ix!=9999 && d.ix!=9999 && a.ix!=d.ix)
merge = false;
if (merge) {
if (a.parentname.Length==0)
a.parentname = d.parentname;
if (a.ix==9999)
a.ix = d.ix;
d = a;
} else // ZAP the old entry in the global lookup (it will still be in the tree)
defs[d.name] = d;
}
if (d.parentname.Length>0) {
Def p = (Def)defs[d.parentname];
if (p==null)
Console.WriteLine("no parent "+a.parentname);
else {
p.nodes[d.ix] = d;
p.names[d.name] = d;
d.parent = p;
}
}
}
}
%symbol Def {
public Def parent = null;
public uint ix = 0;
public string parentname = "";
public string name;
public string help = "";
public System.Collections.Hashtable names = new System.Collections.Hashtable(); // string->Def
public System.Collections.Hashtable nodes = new System.Collections.Hashtable(); // uint->Def
public Def this[string s] { get { return (Def)names[s]; }}
public Def this[uint k] { get { return (Def)nodes[k]; }}
public uint[] path { get { System.Collections.ArrayList a = new System.Collections.ArrayList();
for (Def d=this;d!=null;d=d.parent)
a.Add(d.ix);
uint[] r = new uint[a.Count];
for (int j=0;j<a.Count;j++)
r[j] = (uint)a[a.Count-j-1];
return r;
}}
public string Lookup(uint[] u,int p)
{
string s = name;
if (p>=u.Length)
return s;
Def m = this[u[p]];
if (m!=null)
return s+"."+m.Lookup(u,p+1);
while (p<u.Length)
s += "."+u[p++];
return s;
}
public string[] Kids()
{
string[] r = new string[names.Count];
int j=0;
System.Collections.IDictionaryEnumerator d = names.GetEnumerator();
while (d.MoveNext())
r[j++] = (string)d.Key;
return r;
}
public Def(string i,ObjectType t) {
name=i;
if (t!=null)
help = t.help;
}
public Def(string i,ObjectType t,Oid e) : this(i,t)
{
string b;
string pn = "";
uint n = 0;
for (;e!=null && e.head!=null;e=e.tail) {
b = e.head.name;
n = e.head.val;
if (b.Length>0) {
Def d = (Def)Defs.defs[b];
if (d==null) {
d = new Def(b,null);
d.parentname = pn;
d.ix = n;
Defs.Enter(d);
}
pn = b;
}
}
parentname = pn;
ix = n;
Defs.Enter(this);
}
}
%symbol Attrib {
public string val = null;
public Attrib(string o) { val=Fix(o); }
string Fix(string s)
{
string[] t = s.Split('\n');
string r = "";
for (int j=0;j<t.Length;j++)
r += t[j].Trim()+"\n";
return r;
}
}
%symbol Attribs {
public string help = "";
public Attribs tail;
public Attrib head;
public Attribs(Attrib h,Attribs t) {
head=h; tail=t;
if (h.val!=null)
help = h.val;
else if (tail!=null)
help = tail.help;
}
}
%symbol Oid {
public EnumItem head = null;
public Oid tail;
public Oid(EnumItem h,Oid t) {
head=h; tail=t; }
}
%symbol EnumItem {
public string name;
public uint val;
public EnumItem(string n,uint v) { name=n; val=v;
}
}
Mib : TYPEID DEFINITIONS IS BEGIN Block END;
Block : Imports Exports Defs;
Defs : Def
| Defs Def
;
Def : TYPEID IS Asn Type
| TYPEID IS OBJECT
| ID:n ObjectType:t IS '{' Oid:e '}' %Def(n.id,t,e)
| ID TrapType IS UNSIGNED
| OBJECT MACRO IS BEGIN MacroBody END
;
Asn :
| '[' TYPEID UNSIGNED ']' TYPEID
;
ObjectType : OBJECT Attribs:b %ObjectType(b)
| OBJECT
;
TrapType : TRAP Attribs
| TRAP
;
Attribs : Attrib:a %Attribs(a,null)
| Attribs:t Attrib:a %Attribs(a,t)
;
Type: TYPEID
| CONVENTION Attribs
| SEQUENCE '{' Sequence '}'
| SEQUENCE OF Type
| TYPEID '{' Enum '}'
| TYPEID Size
;
MacroBody : MacroEntry
| MacroBody MacroEntry
;
MacroEntry : TYPEID TYPEID IS MacroStuff
| TYPEID TYPEID IS ID '(' TYPEID TYPEID ')'
| TYPEID IS Values
;
MacroStuff : MacroBit
| MacroStuff MacroBit
;
MacroBit : STRING ID '(' TYPEID TYPEID ')'
| STRING TYPEID
;
Values : STRING
| Values OR STRING
;
Attrib: TYPEID STRING
| TYPEID ID
| MODULE
| DESCRIPTION STRING:s %Attrib(s.yytext)
| SYNTAX Type
| SYNTAX OBJECT
| TYPEID '{' Enum '}'
;
Imports : IMPORTS ImportList ';'
|
;
ImportList : IdList FROM TYPEID
| ImportList IdList FROM TYPEID
;
IdList : ImportId
| IdList ',' ImportId
;
Exports : EXPORTS IdList ';'
|
;
ImportId : ID
| OBJECT
| TYPEID
| CONVENTION
| TRAP
;
Size : '(' SIZE Range ')'
| Range
;
Range : '(' UNSIGNED TO UNSIGNED ')'
| '(' Alt ')'
;
Alt : UNSIGNED
| Alt OR UNSIGNED
;
Sequence : Entry
| Sequence ',' Entry
;
Entry : ID TYPEID
| ID TYPEID Size
| ID TYPEID '{' Enum '}'
| ID OBJECT
;
Enum : EnumItem
| Enum ',' EnumItem
;
EnumItem : ID:a '(' UNSIGNED:b ')' %EnumItem(a.id,b.val)
| ID:a %EnumItem(a.id,(uint)9999)
| UNSIGNED:a %EnumItem("",a.val)
;
Oid : %Oid(null,null)
| EnumItem:a Oid:b %Oid(a,b)
;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -