Ejemplo n.º 1
0
        public void TestEnumerable()
        {
            const string test = "SomeEnum ::= Counter64";
            Lexer lexer = new Lexer();
            StringReader reader = new StringReader(test);
            lexer.Parse(reader);
            string name = lexer.GetNextSymbol().ToString();
            lexer.GetNextSymbol().Expect(Symbol.Assign);
            lexer.GetNextSymbol().Expect(Symbol.Counter64);

            Counter64Type i = new Counter64Type("module", "name", lexer);
        }
Ejemplo n.º 2
0
 public Macro(string module, IList<Symbol> header, Lexer lexer)
 {
     _name = header[0].ToString();
     Symbol temp;
     while ((temp = lexer.GetNextSymbol()) != Symbol.Begin)
     {                
     }
     
     while ((temp = lexer.GetNextSymbol()) != Symbol.End)
     {
     }
 }
Ejemplo n.º 3
0
        public void Test()
        {
            const string test = "SomeIp ::= IpAddress";
            Lexer lexer = new Lexer();
            StringReader reader = new StringReader(test);
            lexer.Parse(reader);
            string name = lexer.GetNextSymbol().ToString();
            lexer.GetNextSymbol().Expect(Symbol.Assign);
            lexer.GetNextSymbol().Expect(Symbol.IpAddress);

            IpAddressType i = new IpAddressType("module", "name", lexer);
        }
Ejemplo n.º 4
0
        public void TestOverlappingRanges3()
        {
            const string test = "SomeEnum ::= INTEGER (8 | 8 | 31 .. 60 )";
            Lexer lexer = new Lexer();
            StringReader reader = new StringReader(test);
            lexer.Parse(reader);
            string name = lexer.GetNextSymbol().ToString();
            lexer.GetNextSymbol().Expect(Symbol.Assign);
            lexer.GetNextSymbol().Expect(Symbol.Integer);

            Assert.Throws<MibException>(() => new IntegerType("module", "name", lexer));
        }
        public Macro(string module, IList <Symbol> header, Lexer lexer)
        {
            _name = header[0].ToString();
            Symbol temp;

            while ((temp = lexer.GetNextSymbol()) != Symbol.Begin)
            {
            }

            while ((temp = lexer.GetNextSymbol()) != Symbol.End)
            {
            }
        }
Ejemplo n.º 6
0
        public void Test()
        {
            const string test = "SomeEnum ::= BITS {first(1), second(2)}";
            Lexer lexer = new Lexer();
            StringReader reader = new StringReader(test);
            lexer.Parse(reader);
            string name = lexer.GetNextSymbol().ToString();
            lexer.GetNextSymbol().Expect(Symbol.Assign);
            lexer.GetNextSymbol().Expect(Symbol.Bits);

            BitsType i = new BitsType("module", "name", lexer);
            Assert.AreEqual("first(1)", i[1]);
        }
Ejemplo n.º 7
0
        public void TestNegative()
        {
            const string test = "SomeEnum ::= OCTET STRING ( SIZE (8 | -5 .. 20 | 31 .. 60 ))";
            Lexer lexer = new Lexer();
            StringReader reader = new StringReader(test);
            lexer.Parse(reader);
            string name = lexer.GetNextSymbol().ToString();
            lexer.GetNextSymbol().Expect(Symbol.Assign);
            lexer.GetNextSymbol().Expect(Symbol.Octet);
            lexer.GetNextSymbol().Expect(Symbol.String);

            Assert.Throws<MibException>(delegate { new OctetStringType("module", "name", lexer); });
        }
Ejemplo n.º 8
0
        public void TestEnumerable()
        {
            const string test = "SomeEnum ::= INTEGER {first(1), second(2)}";
            Lexer lexer = new Lexer();
            StringReader reader = new StringReader(test);
            lexer.Parse(reader);
            string name = lexer.GetNextSymbol().ToString();
            lexer.GetNextSymbol().Expect(Symbol.Assign);
            lexer.GetNextSymbol().Expect(Symbol.Integer);

            IntegerType i = new IntegerType("module", "name", lexer);
            Assert.IsTrue(i.IsEnumeration);
            Assert.AreEqual("first(1)", i[1]);
        }
Ejemplo n.º 9
0
        public MibModule(string name, Lexer lexer)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            
            if (lexer == null)
            {
                throw new ArgumentNullException("lexer");
            }
            
            _name = name.ToUpperInvariant(); // all module name are uppercase.
            Symbol temp = lexer.GetNextNonEOLSymbol();
            temp.Expect(Symbol.Definitions);
            temp = lexer.GetNextNonEOLSymbol();
            temp.Expect(Symbol.Assign);
            temp = lexer.GetNextSymbol();
            temp.Expect(Symbol.Begin);
            temp = lexer.GetNextNonEOLSymbol();
            if (temp == Symbol.Imports)
            {
                _imports = ParseDependents(lexer);
            }
            else if (temp == Symbol.Exports)
            {
                _exports = ParseExports(lexer);
            }
            else if (temp == Symbol.End)
            {
                return;
            }

            ParseEntities(_tokens, temp, _name, lexer);
        }
Ejemplo n.º 10
0
 public TrapType(string module, IList<Symbol> header, Lexer lexer)
 {
     _module = module;
     _name = header[0].ToString();
     Symbol temp;
     while ((temp = lexer.GetNextSymbol()) == Symbol.EOL)
     {
     }
     
     bool succeeded = int.TryParse(temp.ToString(), out _value);
     temp.Assert(succeeded, "not a decimal");
 }
Ejemplo n.º 11
0
        public OctetStringType(string module, string name, Lexer lexer)
        {
            _module = module;
            _name = name;
            _size = new List<ValueRange>();

            Symbol temp = lexer.GetNextSymbol();
            if (temp == Symbol.OpenParentheses)
            {
                _size = DecodeRanges(lexer);
            }

        }
Ejemplo n.º 12
0
        public OctetStringType(string module, string name, Lexer lexer)
        {
            _module = module;
            _name   = name;
            _size   = new List <ValueRange>();

            Symbol temp = lexer.GetNextSymbol();

            if (temp == Symbol.OpenParentheses)
            {
                _size = DecodeRanges(lexer);
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Creates an <see cref="Imports"/> instance.
 /// </summary>
 /// <param name="lexer"></param>
 public Imports(Lexer lexer)
 {
     Symbol temp;
     while ((temp = lexer.GetNextSymbol()) != Symbol.Semicolon)
     {
         if (temp == Symbol.EOL)
         {
             continue;
         }
         
         _dependents.Add(new ImportsFrom(temp, lexer).Module);
     }
 }
Ejemplo n.º 14
0
        public void TestRanges()
        {
            const string test = "SomeEnum ::= OCTET STRING ( SIZE (5 | 8 .. 20 | 31 .. 60 ))";
            Lexer lexer = new Lexer();
            StringReader reader = new StringReader(test);
            lexer.Parse(reader);
            string name = lexer.GetNextSymbol().ToString();
            lexer.GetNextSymbol().Expect(Symbol.Assign);
            lexer.GetNextSymbol().Expect(Symbol.Octet);
            lexer.GetNextSymbol().Expect(Symbol.String);

            OctetStringType i = new OctetStringType("module", "name", lexer);
            Assert.IsTrue(i.Contains(8));
            Assert.IsTrue(i.Contains(5));
            Assert.IsTrue(i.Contains(15));
            Assert.IsTrue(i.Contains(20));
            Assert.IsTrue(i.Contains(35));
            Assert.IsFalse(i.Contains(4));
            Assert.IsFalse(i.Contains(-9));
            Assert.IsFalse(i.Contains(25));
            Assert.IsFalse(i.Contains(61));
        }
Ejemplo n.º 15
0
        public void TestRanges()
        {
            const string test = "SomeEnum ::= INTEGER (8 |10 ..20| 31..60 )";
            Lexer lexer = new Lexer();
            StringReader reader = new StringReader(test);
            lexer.Parse(reader);
            string name = lexer.GetNextSymbol().ToString();
            lexer.GetNextSymbol().Expect(Symbol.Assign);
            lexer.GetNextSymbol().Expect(Symbol.Integer);

            IntegerType i = new IntegerType("module", "name", lexer);
            Assert.IsFalse(i.IsEnumeration);
            Assert.IsTrue(i.Contains(8));
            Assert.IsTrue(i.Contains(10));
            Assert.IsTrue(i.Contains(15));
            Assert.IsTrue(i.Contains(20));
            Assert.IsTrue(i.Contains(35));
            Assert.IsFalse(i.Contains(4));
            Assert.IsFalse(i.Contains(-9));
            Assert.IsFalse(i.Contains(25));
            Assert.IsFalse(i.Contains(61));
        }
Ejemplo n.º 16
0
 public ImportsFrom(Symbol last, Lexer lexer)
 {
     Symbol previous = last;
     Symbol temp;
     while ((temp = lexer.GetNextSymbol()) != Symbol.From)
     {
         if (temp == Symbol.EOL) 
         {
             continue;
         }
         
         if (temp == Symbol.Comma)
         {
             previous.ValidateIdentifier();
             _types.Add(previous.ToString());
         }
         
         previous = temp;
     }
     
     _module = lexer.GetNextSymbol().ToString().ToUpperInvariant(); // module names are uppercase
 }
Ejemplo n.º 17
0
        public TrapType(string module, IList <Symbol> header, Lexer lexer)
        {
            _module = module;
            _name   = header[0].ToString();
            Symbol temp;

            while ((temp = lexer.GetNextSymbol()) == Symbol.EOL)
            {
            }

            bool succeeded = int.TryParse(temp.ToString(), out _value);

            temp.Assert(succeeded, "not a decimal");
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Creates an <see cref="Imports"/> instance.
        /// </summary>
        /// <param name="lexer"></param>
        public Imports(Lexer lexer)
        {
            Symbol temp;

            while ((temp = lexer.GetNextSymbol()) != Symbol.Semicolon)
            {
                if (temp == Symbol.EOL)
                {
                    continue;
                }

                _dependents.Add(new ImportsFrom(temp, lexer).Module);
            }
        }
Ejemplo n.º 19
0
        public ImportsFrom(Symbol last, Lexer lexer)
        {
            Symbol previous = last;
            Symbol temp;

            while ((temp = lexer.GetNextSymbol()) != Symbol.From)
            {
                if (temp == Symbol.EOL)
                {
                    continue;
                }

                if (temp == Symbol.Comma)
                {
                    previous.ValidateIdentifier();
                    _types.Add(previous.ToString());
                }

                previous = temp;
            }

            _module = lexer.GetNextSymbol().ToString().ToUpperInvariant(); // module names are uppercase
        }
Ejemplo n.º 20
0
 public Exports(Lexer lexer)
 {
     Symbol previous = null;
     Symbol temp;
     while ((temp = lexer.GetNextSymbol()) != Symbol.Semicolon) 
     {
         if (temp == Symbol.EOL) 
         {
             continue;
         }
         
         if (temp == Symbol.Comma && previous != null) 
         {
             previous.ValidateIdentifier();
             _types.Add(previous.ToString());
         }
         
         previous = temp;
     }
 }
Ejemplo n.º 21
0
        public Exports(Lexer lexer)
        {
            Symbol previous = null;
            Symbol temp;

            while ((temp = lexer.GetNextSymbol()) != Symbol.Semicolon)
            {
                if (temp == Symbol.EOL)
                {
                    continue;
                }

                if (temp == Symbol.Comma && previous != null)
                {
                    previous.ValidateIdentifier();
                    _types.Add(previous.ToString());
                }

                previous = temp;
            }
        }
Ejemplo n.º 22
0
        private static void ParseEntities(ICollection <IConstruct> tokens, Symbol last, string module, Lexer lexer)
        {
            Symbol         temp   = last;
            IList <Symbol> buffer = new List <Symbol>();

            do
            {
                if (temp == Symbol.Imports || temp == Symbol.Exports || temp == Symbol.EOL)
                {
                    continue;
                }

                buffer.Add(temp);
                if (temp != Symbol.Assign)
                {
                    continue;
                }

                ParseEntity(tokens, module, buffer, lexer);
                buffer.Clear();
            }while ((temp = lexer.GetNextSymbol()) != Symbol.End);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Creates an <see cref="TypeAssignment"/>.
 /// </summary>
 /// <param name="module"></param>
 /// <param name="name"></param>
 /// <param name="lexer"></param>
 /// <param name="last"></param>
 public TypeAssignment(string module, string name, Symbol last, Lexer lexer)
 {
     _module = module;
     _name = name;
     Value = last.ToString();
     
     Symbol temp;
     Symbol veryPrevious = null;
     Symbol previous = last;
     while ((temp = lexer.CheckNextSymbol()) != null)
     {
         if (veryPrevious == Symbol.EOL && previous == Symbol.EOL && temp.ValidateType())
         {
             return;
         }
             
         veryPrevious = previous;
         temp = lexer.GetNextSymbol();
         previous = temp;
     }
     
     previous.Throw("end of file reached");
 }
        /// <summary>
        /// Creates an <see cref="TypeAssignment"/>.
        /// </summary>
        /// <param name="module"></param>
        /// <param name="name"></param>
        /// <param name="lexer"></param>
        /// <param name="last"></param>
        public TypeAssignment(string module, string name, Symbol last, Lexer lexer)
        {
            _module = module;
            _name   = name;
            Value   = last.ToString();

            Symbol temp;
            Symbol veryPrevious = null;
            Symbol previous     = last;

            while ((temp = lexer.CheckNextSymbol()) != null)
            {
                if (veryPrevious == Symbol.EOL && previous == Symbol.EOL && temp.ValidateType())
                {
                    return;
                }

                veryPrevious = previous;
                temp         = lexer.GetNextSymbol();
                previous     = temp;
            }

            previous.Throw("end of file reached");
        }
Ejemplo n.º 25
0
        public MibModule(string name, Lexer lexer)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (lexer == null)
            {
                throw new ArgumentNullException("lexer");
            }

            _name = name.ToUpperInvariant(); // all module name are uppercase.
            Symbol temp = lexer.GetNextNonEOLSymbol();

            temp.Expect(Symbol.Definitions);
            temp = lexer.GetNextNonEOLSymbol();
            temp.Expect(Symbol.Assign);
            temp = lexer.GetNextSymbol();
            temp.Expect(Symbol.Begin);
            temp = lexer.GetNextNonEOLSymbol();
            if (temp == Symbol.Imports)
            {
                _imports = ParseDependents(lexer);
            }
            else if (temp == Symbol.Exports)
            {
                _exports = ParseExports(lexer);
            }
            else if (temp == Symbol.End)
            {
                return;
            }

            ParseEntities(_tokens, temp, _name, lexer);
        }
        public TextualConvention(string module, string name, Lexer lexer)
        {
            _name = name;

            Symbol temp = lexer.GetNextNonEOLSymbol();

            if (temp == Symbol.DisplayHint)
            {
                // TODO: this needs decoding to a useful format.
                _displayHint = new DisplayHint(lexer.GetNextNonEOLSymbol().ToString().Trim(new[] { '"' }));
                temp         = lexer.GetNextNonEOLSymbol();
            }

            temp.Expect(Symbol.Status);
            try
            {
                _status = StatusHelper.CreateStatus(lexer.GetNextNonEOLSymbol().ToString());
                temp    = lexer.GetNextNonEOLSymbol();
            }
            catch (ArgumentException)
            {
                temp.Throw("Invalid status");
            }

            temp.Expect(Symbol.Description);
            _description = lexer.GetNextNonEOLSymbol().ToString().Trim(new[] { '"' });
            temp         = lexer.GetNextNonEOLSymbol();

            if (temp == Symbol.Reference)
            {
                _reference = lexer.GetNextNonEOLSymbol().ToString();
                temp       = lexer.GetNextNonEOLSymbol();
            }

            temp.Expect(Symbol.Syntax);

            /*
             * RFC2579 definition:
             *       Syntax ::=   -- Must be one of the following:
             *                    -- a base type (or its refinement), or
             *                    -- a BITS pseudo-type
             *               type
             *             | "BITS" "{" NamedBits "}"
             *
             * From section 3.5:
             *      The data structure must be one of the alternatives defined
             *      in the ObjectSyntax CHOICE or the BITS construct.  Note
             *      that this means that the SYNTAX clause of a Textual
             *      Convention can not refer to a previously defined Textual
             *      Convention.
             *
             *      The SYNTAX clause of a TEXTUAL CONVENTION macro may be
             *      sub-typed in the same way as the SYNTAX clause of an
             *      OBJECT-TYPE macro.
             *
             * Therefore the possible values are (grouped by underlying type):
             *      INTEGER, Integer32
             *      OCTET STRING, Opaque
             *      OBJECT IDENTIFIER
             *      IpAddress
             *      Counter64
             *      Unsigned32, Counter32, Gauge32, TimeTicks
             *      BITS
             * With appropriate sub-typing.
             */

            temp = lexer.GetNextNonEOLSymbol();
            if (temp == Symbol.Bits)
            {
                _syntax = new BitsType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Integer || temp == Symbol.Integer32)
            {
                _syntax = new IntegerType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Octet)
            {
                temp = lexer.GetNextSymbol();
                temp.Expect(Symbol.String);
                _syntax = new OctetStringType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Opaque)
            {
                _syntax = new OctetStringType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.IpAddress)
            {
                _syntax = new IpAddressType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Counter64)
            {
                _syntax = new Counter64Type(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Unsigned32 || temp == Symbol.Counter32 || temp == Symbol.Gauge32 || temp == Symbol.TimeTicks)
            {
                _syntax = new UnsignedType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Object)
            {
                temp = lexer.GetNextSymbol();
                temp.Expect(Symbol.Identifier);
                _syntax = new ObjectIdentifierType(module, string.Empty, lexer);
            }
            else
            {
                //temp.Throw("illegal syntax for textual convention");
                _syntax = new CustomType(module, string.Empty, lexer);
            }
        }
Ejemplo n.º 27
0
        public TextualConvention(string module, string name, Lexer lexer)
        {
            _name = name;

            Symbol temp = lexer.GetNextNonEOLSymbol();

            if (temp == Symbol.DisplayHint)
            {
                // TODO: this needs decoding to a useful format.
                _displayHint = new DisplayHint(lexer.GetNextNonEOLSymbol().ToString().Trim(new[] { '"' }));
                temp = lexer.GetNextNonEOLSymbol();
            }

            temp.Expect(Symbol.Status);
            try
            {
                _status = StatusHelper.CreateStatus(lexer.GetNextNonEOLSymbol().ToString());
                temp = lexer.GetNextNonEOLSymbol();
            }
            catch (ArgumentException)
            {
                temp.Throw("Invalid status");
            }

            temp.Expect(Symbol.Description);
            _description = lexer.GetNextNonEOLSymbol().ToString().Trim(new[] { '"' });
            temp = lexer.GetNextNonEOLSymbol();

            if (temp == Symbol.Reference)
            {
                _reference = lexer.GetNextNonEOLSymbol().ToString();
                temp = lexer.GetNextNonEOLSymbol();
            }

            temp.Expect(Symbol.Syntax);

            /* 
             * RFC2579 definition:
             *       Syntax ::=   -- Must be one of the following:
             *                    -- a base type (or its refinement), or
             *                    -- a BITS pseudo-type
             *               type
             *             | "BITS" "{" NamedBits "}"
             *
             * From section 3.5:
             *      The data structure must be one of the alternatives defined
             *      in the ObjectSyntax CHOICE or the BITS construct.  Note
             *      that this means that the SYNTAX clause of a Textual
             *      Convention can not refer to a previously defined Textual
             *      Convention.
             *      
             *      The SYNTAX clause of a TEXTUAL CONVENTION macro may be
             *      sub-typed in the same way as the SYNTAX clause of an
             *      OBJECT-TYPE macro.
             * 
             * Therefore the possible values are (grouped by underlying type):
             *      INTEGER, Integer32
             *      OCTET STRING, Opaque
             *      OBJECT IDENTIFIER
             *      IpAddress
             *      Counter64
             *      Unsigned32, Counter32, Gauge32, TimeTicks
             *      BITS
             * With appropriate sub-typing.
             */

            temp = lexer.GetNextNonEOLSymbol();
            if (temp == Symbol.Bits)
            {
                _syntax = new BitsType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Integer || temp == Symbol.Integer32)
            {
                _syntax = new IntegerType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Octet)
            {
                temp = lexer.GetNextSymbol();
                temp.Expect(Symbol.String);
                _syntax = new OctetStringType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Opaque)
            {
                _syntax = new OctetStringType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.IpAddress)
            {
                _syntax = new IpAddressType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Counter64)
            {
                _syntax = new Counter64Type(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Unsigned32 || temp == Symbol.Counter32 || temp == Symbol.Gauge32 || temp == Symbol.TimeTicks)
            {
                _syntax = new UnsignedType(module, string.Empty, lexer);
            }
            else if (temp == Symbol.Object)
            {
                temp = lexer.GetNextSymbol();
                temp.Expect(Symbol.Identifier);
                _syntax = new ObjectIdentifierType(module, string.Empty, lexer);
            }
            else
            {
                //temp.Throw("illegal syntax for textual convention");
                _syntax = new CustomType(module, string.Empty, lexer);
            }
        }
Ejemplo n.º 28
0
        private static void ParseEntities(ICollection<IConstruct> tokens, Symbol last, string module, Lexer lexer)
        {
            Symbol temp = last; 
            IList<Symbol> buffer = new List<Symbol>();
            do
            {
                if (temp == Symbol.Imports || temp == Symbol.Exports || temp == Symbol.EOL)
                {
                    continue;
                }
                
                buffer.Add(temp);
                if (temp != Symbol.Assign)
                {
                    continue;
                }

                ParseEntity(tokens, module, buffer, lexer);
                buffer.Clear();
            }
            while ((temp = lexer.GetNextSymbol()) != Symbol.End);
        }