Example #1
0
        /// <summary>
        /// Loads a MIB file.
        /// </summary>
        public static IEnumerable <IModule> Compile(Stream stream, List <CompilerError> errors, List <CompilerWarning> warnings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            var lexer  = new SmiLexer(new ANTLRInputStream(stream));
            var tokens = new CommonTokenStream(lexer);
            var parser = new SmiParser(tokens);

            try
            {
                var doc = parser.GetDocument(string.Empty);
#if !MA
                Logger.Info(watch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture) + "-ms used to parse");
#endif
                watch.Stop();
                errors.AddRange(parser.Errors);
                warnings.AddRange(parser.Warnings);
                return(doc.Modules.OfType <IModule>().ToList());
            }
            catch (RecognitionException ex)
            {
                errors.Add(new CompilerError(ex));
                errors.AddRange(parser.Errors);
                warnings.AddRange(parser.Warnings);
                return(new IModule[0]);
            }
        }
Example #2
0
        /// <summary>
        /// Loads a MIB file.
        /// </summary>
        /// <param name="fileName">File name.</param>
        /// <param name="errors">Errors.</param>
        public static IEnumerable <IModule> Compile(string fileName, List <CompilerError> errors, List <CompilerWarning> warnings)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            if (fileName.Length == 0)
            {
                throw new ArgumentException("fileName cannot be empty");
            }

            if (!File.Exists(fileName))
            {
                throw new ArgumentException("file does not exist: " + fileName);
            }

            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            var lexer  = new SmiLexer(new ANTLRFileStream(fileName));
            var tokens = new CommonTokenStream(lexer);
            var parser = new SmiParser(tokens);

            try
            {
                var doc = parser.GetDocument(fileName);
#if !MA
                Logger.Info(watch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture) + "-ms used to parse " +
                            fileName);
#endif
                watch.Stop();
                errors.AddRange(parser.Errors);
                warnings.AddRange(parser.Warnings);
                return(doc.Modules.OfType <IModule>());
            }
            catch (RecognitionException ex)
            {
                errors.Add(new CompilerError(ex)
                {
                    FileName = fileName
                });
                errors.AddRange(parser.Errors);
                warnings.AddRange(parser.Warnings);
                return(new IModule[0]);
            }
        }
        internal static bool IsValidIdentifier(this IToken token, SmiParser parser)
        {
            string message;
            string name = token.Text;

            if (UseStricterValidation && (name.Length < 1 || name.Length > 64))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                    "warning N0006 : an identifier must consist of 1 to 64 letters, digits, and hyphens. {0}",
                    token.Text)
                              : string.Format(
                    "{0} ({1},{2}) : warning N0006 : an identifier must consist of 1 to 64 letters, digits, and hyphens. {3}",
                    parser.FileName,
                    token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return(false);
            }

            if (!Char.IsLetter(name[0]))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                    "warning N0007 : the initial character must be a letter. {0}", token.Text)
                              : string.Format(
                    "{0} ({1},{2}) : warning N0007 : the initial character must be a letter. {3}",
                    parser.FileName,
                    token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return(false);
            }

            if (name.EndsWith("-", StringComparison.Ordinal))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                    "warning N0008 : a hyphen cannot be the last character of an identifier. {0}",
                    token.Text)
                              : string.Format(
                    "{0} ({1},{2}) : warning N0008 : a hyphen cannot be the last character of an identifier. {3}",
                    parser.FileName,
                    token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return(false);
            }

            if (name.Contains("--"))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                    "warning N0009 : a hyphen cannot be immediately followed by another hyphen in an identifier. {0}",
                    token.Text)
                              : string.Format(
                    "{0} ({1},{2}) : warning N0009 : a hyphen cannot be immediately followed by another hyphen in an identifier. {3}",
                    parser.FileName,
                    token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return(false);
            }

            if (UseStricterValidation && name.Contains("_"))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                    "warning N0010 : underscores are not allowed in identifiers. {0}", token.Text)
                              : string.Format(
                    "{0} ({1},{2}) : warning N0010 : underscores are not allowed in identifiers. {3}",
                    parser.FileName,
                    token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return(false);
            }

            // TODO: SMIv2 forbids "-" except in module names and keywords
            return(true);
        }
Example #4
0
        internal static bool IsValidIdentifier(this IToken token, SmiParser parser)
        {
            string message;
            string name = token.Text;
            if (UseStricterValidation && (name.Length < 1 || name.Length > 64))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                                  "warning N0006 : an identifier must consist of 1 to 64 letters, digits, and hyphens. {0}",
                                  token.Text)
                              : string.Format(
                                  "{0} ({1},{2}) : warning N0006 : an identifier must consist of 1 to 64 letters, digits, and hyphens. {3}",
                                  parser.FileName,
                                  token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return false;
            }

            if (!Char.IsLetter(name[0]))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                                  "warning N0007 : the initial character must be a letter. {0}", token.Text)
                              : string.Format(
                                  "{0} ({1},{2}) : warning N0007 : the initial character must be a letter. {3}",
                                  parser.FileName,
                                  token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return false;
            }

            if (name.EndsWith("-", StringComparison.Ordinal))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                                  "warning N0008 : a hyphen cannot be the last character of an identifier. {0}",
                                  token.Text)
                              : string.Format(
                                  "{0} ({1},{2}) : warning N0008 : a hyphen cannot be the last character of an identifier. {3}",
                                  parser.FileName,
                                  token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return false;
            }

            if (name.Contains("--"))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                                  "warning N0009 : a hyphen cannot be immediately followed by another hyphen in an identifier. {0}",
                                  token.Text)
                              : string.Format(
                                  "{0} ({1},{2}) : warning N0009 : a hyphen cannot be immediately followed by another hyphen in an identifier. {3}",
                                  parser.FileName,
                                  token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return false;
            }

            if (UseStricterValidation && name.Contains("_"))
            {
                message = string.IsNullOrEmpty(parser.FileName)
                              ? string.Format(
                                  "warning N0010 : underscores are not allowed in identifiers. {0}", token.Text)
                              : string.Format(
                                  "{0} ({1},{2}) : warning N0010 : underscores are not allowed in identifiers. {3}",
                                  parser.FileName,
                                  token.Line, token.CharPositionInLine + 1, token.Text);
                parser.Warnings.Add(new CompilerWarning(token, parser.FileName, message));
                return false;
            }

            // TODO: SMIv2 forbids "-" except in module names and keywords
            return true;
        }
Example #5
0
        /// <summary>
        /// Loads a MIB file.
        /// </summary>
        /// <param name="fileName">File name.</param>
        /// <param name="errors">Errors.</param>
        public static IEnumerable<IModule> Compile(string fileName, List<CompilerError> errors, List<CompilerWarning> warnings)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            if (fileName.Length == 0)
            {
                throw new ArgumentException("fileName cannot be empty");
            }

            if (!File.Exists(fileName))
            {
                throw new ArgumentException("file does not exist: " + fileName);
            }

            var watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            var lexer = new SmiLexer(new ANTLRFileStream(fileName));
            var tokens = new CommonTokenStream(lexer);
            var parser = new SmiParser(tokens);
            try
            {
                var doc = parser.GetDocument(fileName);
#if !MA && !MT
                Logger.Info(watch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture) + "-ms used to parse " +
                            fileName);
#endif
                watch.Stop();
                errors.AddRange(parser.Errors);
                warnings.AddRange(parser.Warnings);
                return doc.Modules.OfType<IModule>();
            }
            catch (RecognitionException ex)
            {
                errors.Add(new CompilerError(ex, fileName));
                errors.AddRange(parser.Errors);
                warnings.AddRange(parser.Warnings);
                return new IModule[0];
            }
        }
Example #6
0
        /// <summary>
        /// Loads a MIB file.
        /// </summary>
        public static IEnumerable<IModule> Compile(Stream stream, List<CompilerError> errors, List<CompilerWarning> warnings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            var lexer = new SmiLexer(new ANTLRInputStream(stream));
            var tokens = new CommonTokenStream(lexer);
            var parser = new SmiParser(tokens);
            try
            {
                var doc = parser.GetDocument(string.Empty);
#if !MA && !MT
                Logger.Info(watch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture) + "-ms used to parse");
#endif
                watch.Stop();
                errors.AddRange(parser.Errors);
                warnings.AddRange(parser.Warnings);
                return doc.Modules.OfType<IModule>().ToList();
            }
            catch (RecognitionException ex)
            {
                errors.Add(new CompilerError(ex, null));
                errors.AddRange(parser.Errors);
                warnings.AddRange(parser.Warnings);
                return new IModule[0];
            }
        }