示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.Parameter"/> class.
 /// </summary>
 /// <param name="token">The token source.</param>
 /// <param name="direction">The parameter direction.</param>
 /// <param name="name">The parameter name.</param>
 /// <param name="index">The optional index</param>
 /// <param name="explicttype">The explict type, if any</param>
 public Parameter(ParseToken token, ParameterDirection direction, Identifier name, TypeName explicittype)
     : base(token)
 {
     this.Name        = name ?? throw new ArgumentNullException(nameof(name));
     this.Direction   = direction;
     this.ExplictType = explicittype;
 }
示例#2
0
文件: Module.cs 项目: kenkendk/SMEIL
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.Module"/> class.
 /// </summary>
 /// <param name="token">The source token</param>
 /// <param name="imports">The imports in the module.</param>
 /// <param name="declarations">The typedefinitions in the module.</param>
 /// <param name="entities">The entities in the module.</param>
 public Module(ParseToken token, ImportStatement[] imports, Declaration[] declarations, Entity[] entities)
     : base(token)
 {
     Imports      = imports;
     Entities     = entities;
     Declarations = declarations;
 }
示例#3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.DataType"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 /// <param name="parent">The enumeration type.</param>
 public DataType(ParseToken token, AST.EnumDeclaration parent)
     : base(token)
 {
     Type     = ILType.Enumeration;
     BitWidth = -1;
     EnumType = parent ?? throw new ArgumentNullException(nameof(parent));
 }
示例#4
0
        /// <summary>
        /// Builds a shape for a given identifier within the process
        /// </summary>
        /// <param name="source">The source token</param>
        /// <param name="signals">The signals in the bus</param>
        public BusShape(ParseToken source, IEnumerable <AST.BusSignalDeclaration> signals)
            : base(source)
        {
            if (signals == null)
            {
                throw new ArgumentNullException(nameof(signals));
            }

            var duplicates = signals
                             .GroupBy(x => x.Name.Name)
                             .Where(x => x.Count() > 1)
                             .Select(x => x.ToList())
                             .FirstOrDefault();

            if (duplicates != null)
            {
                // Fix the order to report the first instance
                duplicates =
                    duplicates
                    .OrderBy(x => x.SourceToken.CharOffset)
                    .ToList();

                throw new ParserException($"Multiple signals with the name \"{duplicates.First().Name.Name}\": {string.Join(", ", duplicates.Select(x => x.SourceToken.ToString()))}", duplicates.First());
            }

            Signals = signals.ToDictionary(x => x.Name.Name, x => new BusShapeValue(x.Type, x.Direction));
            if (Signals.Count == 0)
            {
                throw new ParserException("Cannot have an empty set of signals in a bus shape", source);
            }
        }
示例#5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.DataType"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 /// <param name="shape">The bus shape.</param>
 public DataType(ParseToken token, BusShape shape)
     : base(token)
 {
     Type     = ILType.Bus;
     BitWidth = -1;
     Shape    = shape ?? throw new ArgumentNullException(nameof(shape));
 }
示例#6
0
 /// <summary>
 /// Constructs a new constant declaration
 /// </summary>
 /// <param name="token">The token to use</param>
 /// <param name="name">The name of the constant</param>
 /// <param name="dataType">The type of the constant</param>
 /// <param name="expression">The value of the constant element</param>
 public ConstantDeclaration(ParseToken token, Identifier name, TypeName dataType, Expression expression)
     : base(token)
 {
     Name       = name ?? throw new ArgumentNullException(nameof(name));
     DataType   = dataType ?? throw new ArgumentNullException(nameof(dataType));
     Expression = expression ?? throw new ArgumentNullException(nameof(expression));
 }
示例#7
0
 /// <summary>
 /// Creates a new instance declaration
 /// </summary>
 /// <param name="token">The token used to create the instance declaration</param>
 /// <param name="name">The name of this instance</param>
 /// <param name="sourceItem">The item being instantiated</param>
 /// <param name="parameters">The parameters to instantiate</param>
 public InstanceDeclaration(ParseToken token, InstanceName name, Identifier sourceItem, ParameterMap[] parameters)
     : base(token)
 {
     Name       = name ?? throw new ArgumentNullException(nameof(name));
     SourceItem = sourceItem ?? throw new ArgumentNullException(nameof(sourceItem));
     Parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
 }
示例#8
0
文件: Match.cs 项目: kenkendk/SMEIL
 /// <summary>
 /// Constructs a new match
 /// </summary>
 /// <param name="token">The token that matched</param>
 /// <param name="item">The item that matched</param>
 /// <param name="subMatches">Submatches</param>
 public Match(BNFItem token, ParseToken item, Match[] subMatches, bool matched)
 {
     Token      = token;
     Item       = item;
     SubMatches = subMatches;
     Matched    = matched;
 }
示例#9
0
 /// <summary>
 /// Constructs a new binary expression
 /// </summary>
 /// <param name="token">The parse token</param>
 /// <param name="left">The left-hand-side expression</param>
 /// <param name="operation">The operation</param>
 /// <param name="right">The right-hand-side expression</param>
 public BinaryExpression(ParseToken token, Expression left, BinaryOperation operation, Expression right)
     : base(token)
 {
     Left      = left ?? throw new ArgumentNullException(nameof(left));
     Operation = operation ?? throw new ArgumentNullException(nameof(operation));
     Right     = right ?? throw new ArgumentNullException(nameof(right));
 }
示例#10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.Variable"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 /// <param name="name">The name of the variable.</param>
 /// <param name="type">The data type.</param>
 /// <param name="initializer">The initializer expression</param>
 public VariableDeclaration(ParseToken token, Identifier name, TypeName type, Expression initializer)
     : base(token)
 {
     Name        = name ?? throw new ArgumentNullException(nameof(name));
     Type        = type ?? throw new ArgumentNullException(nameof(type));
     Initializer = initializer;
 }
示例#11
0
文件: Network.cs 项目: kenkendk/SMEIL
 /// <summary>
 /// Creates a new parsed network instance
 /// </summary>
 /// <param name="name">The name of the instance</param>
 public Network(ParseToken token, Identifier name, Parameter[] parameters, NetworkDeclaration[] declarations)
     : base(token)
 {
     Name         = name ?? throw new ArgumentNullException(nameof(name));;
     Parameters   = parameters ?? new Parameter[0];
     Declarations = declarations ?? throw new ArgumentNullException(nameof(declarations));
 }
示例#12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.Import"/> class.
 /// </summary>
 /// <param name="token">The source token</param>
 /// <param name="localname">The import name.</param>
 public ImportStatement(ParseToken token, ImportName modulename, Identifier[] sourceNames, Identifier localname)
     : base(token)
 {
     ModuleName  = modulename;
     SourceNames = sourceNames;
     LocalName   = localname;
 }
示例#13
0
 /// <summary>
 /// Constructs a new typecast expression
 /// </summary>
 /// <param name="token">The parse token</param>
 /// <param name="expression">The expression inside the parenthesis</param>
 /// <param name="targetype">The target type for the typecast</param>
 /// <param name="explicit">A value indicating if the typecast is explicit or implicit</param>
 public TypeCast(ParseToken token, Expression expression, DataType targettype, bool @explicit)
     : base(token)
 {
     Expression = expression ?? throw new ArgumentNullException(nameof(expression));
     TargetName = new TypeName(targettype, null);
     Explicit   = @explicit;
 }
示例#14
0
 public static IParser <SqlExpression> Symbol(string text)
 {
     return(ParseToken.Symbol(text)
            .MapResult(x => new SqlExpression()
     {
         TextSpan = x
     }));
 }
示例#15
0
 /// <summary>
 /// Creates a new generator declaration
 /// </summary>
 /// <param name="source">The source token</param>
 /// <param name="name">The generator name</param>
 /// <param name="sourceExpression">The source expression</param>
 /// <param name="targetExpression">The target expression</param>
 /// <param name="networks">The generator networks</param>
 public GeneratorDeclaration(ParseToken source, Identifier name, Expression sourceExpression, Expression targetExpression, NetworkDeclaration[] networks)
     : base(source)
 {
     Name             = name;
     SourceExpression = sourceExpression;
     TargetExpression = targetExpression;
     Networks         = networks;
 }
示例#16
0
 public static IParser <SqlExpression> Symbols(params string[] texts)
 {
     return(Lexeme(ParseToken.Symbols(texts))
            .MapResult(x => new SqlExpression()
     {
         TextSpan = x
     }));
 }
示例#17
0
 public static IParser <SqlExpression> Word(string text)
 {
     return(Lexeme(ParseToken.Match(text))
            .MapResult(x => new SqlExpression()
     {
         TextSpan = x
     }));
 }
示例#18
0
 /// <summary>
 /// Creates a new bus signal declaration
 /// </summary>
 /// <param name="source">The token source</param>
 /// <param name="name">The signal name</param>
 /// <param name="type">The signal type</param>
 /// <param name="initializer">The optional initializer</param>
 /// <param name="direction">The optional signal direction</param>
 public BusSignalDeclaration(ParseToken source, Identifier name, TypeName type, Expression initializer, SignalDirection direction)
     : base(source)
 {
     Name        = name;
     Type        = type;
     Initializer = initializer;
     Direction   = direction;
 }
示例#19
0
 /// <summary>
 /// Constructs a new if statement
 /// </summary>
 /// <param name="token">The token where the statement was found</param>
 /// <param name="condition">The condition expression</param>
 /// <param name="trueStatements">The truth statements</param>
 /// <param name="elifstatements">The optional else-if statements</param>
 /// <param name="falseStatements">The false statements</param>
 public IfStatement(ParseToken token, Expression condition, Statement[] trueStatements, Tuple <Expression, Statement[]>[] elifstatements, Statement[] falseStatements)
     : base(token)
 {
     Condition       = condition ?? throw new ArgumentNullException(nameof(condition));
     TrueStatements  = trueStatements;
     FalseStatements = falseStatements;
     ElIfStatements  = elifstatements;
 }
示例#20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.DataType"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 /// <param name="parent">The array data type.</param>
 public DataType(ParseToken token, DataType parent)
     : base(token)
 {
     Type     = parent.Type;
     BitWidth = parent.BitWidth;
     Shape    = parent.Shape;
     EnumType = parent.EnumType;
 }
示例#21
0
 /// <summary>
 /// Constructs a new for statement
 /// </summary>
 /// <param name="token">The parse token</param>
 /// <param name="variable">The loop variable</param>
 /// <param name="fromExpression">The from expression</param>
 /// <param name="toExpression">The to expression</param>
 /// <param name="statements">The statements in the loop body</param>
 public ForStatement(ParseToken token, VariableDeclaration variable, Expression fromExpression, Expression toExpression, Statement[] statements)
     : base(token)
 {
     Variable       = variable ?? throw new ArgumentNullException(nameof(variable));
     FromExpression = fromExpression ?? throw new ArgumentNullException(nameof(fromExpression));
     ToExpression   = toExpression ?? throw new ArgumentNullException(nameof(toExpression));
     Statements     = statements ?? throw new ArgumentNullException(nameof(statements));
 }
示例#22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.DataType"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 /// <param name="width">The length of the array</param>
 /// <param name="SourceConstExpression">The constant expression defining the length of the array</param>
 /// <param name="elementType">The element type.</param>
 public DataType(ParseToken token, int width, Expression sourceConstExpr, DataType elementType)
     : base(token)
 {
     Type     = ILType.Array;
     BitWidth = width;
     SourceConstExpression = sourceConstExpr;
     ElementType           = elementType ?? throw new ArgumentNullException(nameof(elementType));
 }
示例#23
0
 /// <summary>
 /// Parses a function definition
 /// </summary>
 /// <param name="token">The source token</param>
 /// <param name="name">The name of the function</param>
 /// <param name="parameters">The function parameters</param>
 /// <param name="statements">The statements in the function</param>
 public FunctionDefinition(ParseToken token, Identifier name, Parameter[] parameters, Declaration[] declarations, Statement[] statements)
     : base(token)
 {
     Name         = name ?? throw new ArgumentNullException(nameof(name));
     Parameters   = parameters ?? throw new ArgumentNullException(nameof(parameters));
     Statements   = statements ?? throw new ArgumentNullException(nameof(statements));
     Declarations = declarations ?? throw new ArgumentNullException(nameof(declarations));
 }
示例#24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.Identifier"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 public Identifier(ParseToken token)
     : base(token)
 {
     if (!IsValidIdentifier(Name))
     {
         throw new ArgumentException($"Invalid identifier {Name}", nameof(Name));
     }
 }
示例#25
0
 /// <summary>
 /// Creates a new connect declaration
 /// </summary>
 /// <param name="item">The source token</param>
 /// <param name="source">The source identifier</param>
 /// <param name="target">The target identifier</param>
 public ConnectDeclaration(ParseToken item, ConnectEntry[] entries)
     : base(item)
 {
     this.Entries = entries ?? throw new ArgumentNullException(nameof(entries));
     if (this.Entries.Length == 0)
     {
         throw new ArgumentException("Cannot have an empty connect declaration", nameof(entries));
     }
 }
示例#26
0
文件: Process.cs 项目: kenkendk/SMEIL
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.Process"/> class.
 /// </summary>
 /// <param name="token">The source token</param>
 /// <param name="clocked">If set to <c>true</c>, the process is clocked.</param>
 /// <param name="name">The process name.</param>
 /// <param name="parameters">The parameters</param>
 /// <param name="declarations">The declaration statements.</param>
 /// <param name="statements">The body statements.</param>
 public Process(ParseToken token, bool clocked, Identifier name, Parameter[] parameters, Declaration[] declarations, Statement[] statements)
     : base(token)
 {
     Clocked      = clocked;
     Name         = name ?? throw new ArgumentNullException(nameof(name));
     Parameters   = parameters ?? new Parameter[0];
     Declarations = declarations ?? throw new ArgumentNullException(nameof(declarations));
     Statements   = statements ?? throw new ArgumentNullException(nameof(statements));
 }
示例#27
0
 /// <summary>
 /// Builds a shape for a given identifier within the process
 /// </summary>
 /// <param name="source">The source token</param>
 /// <param name="contents">The signals in the bus</param>
 public BusShape(ParseToken source, IDictionary <string, BusShapeValue> contents)
     : base(source)
 {
     Signals = new Dictionary <string, BusShapeValue>(contents);
     if (Signals.Count == 0)
     {
         throw new ParserException("Cannot have an empty set of signals in a bus shape", source);
     }
 }
示例#28
0
 /// <summary>
 /// Constructs a new switch statement
 /// </summary>
 /// <param name="token">The parsed token</param>
 /// <param name="value">The value to switch on</param>
 /// <param name="cases">The cases in the switch</param>
 public SwitchStatement(ParseToken token, Expression value, Tuple <Expression, Statement[]>[] cases)
     : base(token)
 {
     Value = value ?? throw new ArgumentNullException(nameof(value));
     Cases = cases ?? throw new ArgumentNullException(nameof(cases));
     if (Cases.Length == 0)
     {
         throw new ParserException($"A switch statement must have at least one case", token);
     }
 }
示例#29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:SMEIL.Parser.AST.EnumDefinition"/> class.
 /// </summary>
 /// <param name="token">The source token.</param>
 /// <param name="name">The enum name.</param>
 /// <param name="fields">The enum fields.</param>
 public EnumDeclaration(ParseToken token, Identifier name, EnumField[] fields)
     : base(token)
 {
     Name   = name ?? throw new ArgumentNullException(nameof(name));
     Fields = fields ?? throw new ArgumentNullException(nameof(fields));
     if (Fields.Length == 0)
     {
         throw new ParserException($"Enumerations must have at least one field", token);
     }
 }
示例#30
0
 /// <summary>
 /// Constructs a new type definition for an alias
 /// </summary>
 /// <param name="source">The source parse token</param>
 /// <param name="name">The name of this type definition</param>
 /// <param name="alias">The alias to use</param>
 public TypeDefinition(ParseToken source, Identifier name, TypeName alias)
     : base(source)
 {
     Name  = name ?? throw new ArgumentNullException(nameof(name));
     Alias = alias ?? throw new ArgumentNullException(nameof(alias));
     if (string.IsNullOrWhiteSpace(Name.Name))
     {
         throw new ParserException($"The name of a type definition cannot be anonymous", source);
     }
 }
示例#31
0
 protected abstract int Parse(ParseToken token);
示例#32
0
 protected virtual void PostParse(ParseToken token)
 {
 }
示例#33
0
        private void ParsePartialInput()
        {
            int pos = 0;
            char current;
            int oldstate = -1;

            // local variables: faster access.
            int state = currentState.Type;
            bool lastEoline = currentState.NewlineSeen;

            StringBuilder input = currentState.InputBuffer;
            StringBuilder parsed = currentState.CommandBuffer;

            if (state == (int) TokenType.NewStatement) {
                parsed.Length = 0;
                // skip leading whitespaces of next statement...
                while (pos < input.Length &&
                    Char.IsWhiteSpace(input[pos])) {
                    //CHECK: what about \r?
                    currentState.NewlineSeen = (input[pos] == '\n');
                    ++pos;
                }
                input.Remove(0, pos);
                pos = 0;
            }

            if (input.Length == 0)
                state = (int) TokenType.PotentialEndFound;

            while (state != (int) TokenType.PotentialEndFound &&
                pos < input.Length) {
                bool reIterate;
                current = input[pos];
                if (current == '\r')
                    current = '\n'; // canonicalize.

                if (current == '\n')
                    currentState.NewlineSeen = true;

                do {
                    reIterate = false;
                    ParseToken token = new ParseToken(state, current, lastEoline);
                    state = Parse(token);

                    if (token.NewLineSeenWasSet)
                        lastEoline = token.NewLineSeen;

                    if (token.AppendedCharacter != '\0')
                        parsed.Append(token.AppendedCharacter);

                    if (token.ContinueParsingWasSet)
                        reIterate = token.ContinueParsing;
                } while (reIterate);

                ParseToken postToken = new ParseToken(oldstate, current, lastEoline);
                PostParse(postToken);

                if (postToken.AppendedCharacter != '\0')
                    parsed.Append(postToken.AppendedCharacter);

                oldstate = state;
                pos++;
                // we maintain the state of 'just seen newline' as long
                // as we only skip whitespaces..
                lastEoline &= Char.IsWhiteSpace(current);
            }

            // we reached: POTENTIAL_END_FOUND. Store the rest, that
            // has not been parsed in the input-buffer.
            input.Remove(0, pos);
            currentState.Type = state;
        }