Пример #1
0
        public static FunctionCallNode Read(ReadParams p, Span funcNameSpan, string funcName, Definition funcDef = null)
        {
            if (funcDef != null)
            {
                var node = ParseArguments(p, funcNameSpan, funcName, funcDef);
                if (node != null)
                {
                    return(node);
                }
            }

            var funcDefs = (from d in p.Statement.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetAny(funcNameSpan.Start, funcName)
                            where d.ArgumentsRequired && !d.RequiresParent(p.CodeAnalyzer.CodeModel.ClassName)
                            select d).ToArray();

            foreach (var def in funcDefs)
            {
                var fd = def as FunctionDefinition;
                if (fd == null)
                {
                    continue;
                }

                var node = ParseArguments(p, funcNameSpan, funcName, fd);
                if (node != null)
                {
                    return(node);
                }
            }

            var funcCallNode = new FunctionCallNode(p.Statement, funcNameSpan, funcName, null);

            funcCallNode.ReportError(funcNameSpan, CAError.CA0003, funcName);                   // Function '{0}' not found.
            return(funcCallNode);
        }
Пример #2
0
        private void ParseTable(ReadParams readParams)
        {
            byte[] plainText;
#if USE_TABLE_XC
            PTGame.FrameworkgFile PTGame.FrameworkgFile = new PTGame.FrameworkgFile();
            PTGame.FrameworkgFile.Read(readParams.fileData);

            if (!m_CryptoHelper.RsaVerify(PTGame.FrameworkgFile.FileData, PTGame.FrameworkgFile.RasText))
            {
                Log.iError("RsaVerify Fail");
            }
            byte[] plainZipText = m_CryptoHelper.DesDecrypt(PTGame.FrameworkgFile.FileData);
            //解压
            using (MemoryStream ms = new MemoryStream(plainZipText))
            {
                using (ZipFile zipFile = new ZipFile(ms))
                {
                    ZipEntry zipEntry = zipFile[0];
                    plainText = new byte[zipEntry.Size];
                    var stream = zipFile.GetInputStream(zipEntry);
                    stream.Read(plainText, 0, plainText.Length);
                    zipFile.Close();
                }
                ms.Close();
            }
#else
            plainText = readParams.fileData;
            #endif
            ReadTable(readParams.tableInfo, plainText);
        }
Пример #3
0
            //FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
            //	Read
            //========================================================================
#if true
            public int Read(byte[] buff, int buffOffset, long offset, ref int rlength)
            {
                // ファイルサイズ制限
                if (offset + rlength > filesize)
                {
                    rlength = (int)(filesize - offset);
                }

                ReadParams p = new ReadParams(this, buff, buffOffset, offset, rlength);

                lock (map){
                    if (p.triml_cached())
                    {
                        rlength = p.ReadLength;
                        return(0);
                    }

                    p.accel_seq_access();
                    p.trimr_cached();

                    // データ取得
                    p.download_data();
                    rlength = p.ReadLength;
                    return(0);
                }
            }
Пример #4
0
        public WhileStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);

            _cond = ExpressionNode.Read(p, null);
            if (_cond == null)
            {
                ReportError(keywordSpan, CAError.CA0018, "if");                 // Expected condition after '{0}'.
                return;
            }

            if (!p.Code.ReadExact("{"))
            {
                ReportError(keywordSpan, CAError.CA0019);                       // Expected '{'.
                return;
            }

            while (!p.Code.EndOfFile && !p.Code.ReadExact("}"))
            {
                var stmt = Statement.Read(p);
                if (stmt == null)
                {
                    break;
                }
                _body.Add(stmt);
            }
        }
Пример #5
0
 public BreakStatement(ReadParams p, Span keywordSpan)
     : base(p.CodeAnalyzer, keywordSpan)
 {
     if (!p.Code.ReadExact(';'))
     {
         ReportError(p.Code.Span, CAError.CA0015);                                       // Expected ';'.
     }
 }
Пример #6
0
        private ReadParams CreateReadParams(TDTableMetaData tdTableMetaData)
        {
            ReadParams readParam = new ReadParams();

            readParam.filePath        = GetTableFilePath(tdTableMetaData.TableName);
            readParam.tdTableMetaData = tdTableMetaData;
            return(readParam);
        }
Пример #7
0
        public ExtractStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);
            var code = p.Code;

            code.ReadExactWholeWord("permanent");               // Optional

            if (!code.ReadWord())
            {
                ReportError(keywordSpan, CAError.CA0044);                       // Expected temp table name to follow 'extract'.
                return;
            }
            var tableName = code.Text;

            var def = p.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetGlobalFromFile <ExtractTableDefinition>(tableName).FirstOrDefault();

            if (def == null)
            {
                ReportError(code.Span, CAError.CA0045, tableName);                      // Extract table '{0}' does not exist.
                return;
            }

            while (!code.EndOfFile)
            {
                if (!code.ReadWord())
                {
                    ReportError(new Span(code.Position, code.Position + 1), CAError.CA0046);                            // Expected extract column name.
                    return;
                }
                var colDef = def.GetChildDefinitions(code.Text).FirstOrDefault();
                if (colDef == null)
                {
                    ReportError(code.Span, CAError.CA0046);                     // Expected extract column name.
                    return;
                }

                if (!code.ReadExact('='))
                {
                    ReportError(new Span(code.Position, code.Position + 1), CAError.CA0047);                            // Expected '=' to follow extract column name.
                    return;
                }

                var exp = ExpressionNode.Read(p, null, true);
                if (exp == null)
                {
                    ReportError(new Span(code.Position, code.Position + 1), CAError.CA0048);                            // Expected extract column expression.
                    return;
                }

                _colExps.Add(exp);

                if (code.ReadExact(';'))
                {
                    return;
                }
            }
        }
Пример #8
0
        private static ReadParams CreateReadParams(Hunter.DTableOnParse onParse, string tableName)
        {
            ReadParams readParam = new ReadParams();

            readParam.filePath           = TableHelper.GetTableFilePath(tableName);
            readParam.tableInfo          = new TableInfo(onParse, tableName);
            readParam.tableInfo.fileName = tableName;
            readParam.tableInfo.parseRun = onParse;
            return(readParam);
        }
Пример #9
0
        private bool ReadTableColOrRelInd(ReadParams p, bool allowIndexes)
        {
            var code = p.Code;

            if (!code.ReadWord())
            {
                ReportError(new Span(code.Position, code.Position + 1), CAError.CA0038);                        // Expected table or relationship name.
                return(false);
            }

            var def = _tables.FirstOrDefault(x => x.Name == code.Text);

            if (def == null)
            {
                def = CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetGlobalFromAnywhere <RelIndDefinition>(code.Text).FirstOrDefault();
                if (def == null && allowIndexes)
                {
                    ReportError(code.Span, CAError.CA0037, code.Text);                          // Table or relationship '{0}' is not referenced in the 'from' clause.
                    return(false);
                }
            }

            Definition tableDef = def as TableDefinition;

            if (tableDef == null)
            {
                tableDef = def as ExtractTableDefinition;
            }
            if (tableDef != null)
            {
                if (!code.ReadExact('.'))
                {
                    ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, ".");                       // Expected '{0}'.
                    return(false);
                }

                if (!code.ReadWord())
                {
                    ReportError(new Span(code.Position, code.Position + 1), CAError.CA0039);                            // Expected column name to follow table name.
                    return(false);
                }

                var fieldDef = tableDef.GetChildDefinitions(code.Text).FirstOrDefault();
                if (fieldDef == null)
                {
                    ReportError(code.Span, CAError.CA0040, tableDef.Name, code.Text);                           // Table '{0}' has no column '{1}'.
                    return(false);
                }
            }

            return(true);
        }
Пример #10
0
        public ColStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);
            var code = p.Code;

            if (!code.ReadExact('+'))
            {
                code.ReadExact('-');
            }
            code.ReadNumber();

            code.ReadExact(';');                // Optional
        }
Пример #11
0
 private void ReadTable(ReadParams readparm)
 {
     if (readparm != null)
     {
         try
         {
             readparm.tdTableMetaData.OnParse(readparm.fileData);
         }
         catch (System.Exception ex)
         {
             Log.E("Parse table error TD" + readparm.tdTableMetaData.TableName);
             Log.E(ex.ToString() + ex.StackTrace);
         }
     }
 }
Пример #12
0
        private bool ReadGroupStatements(ReadParams p, bool isDefault)
        {
            var body = new GroupBody {
                isDefault = isDefault
            };
            var code = p.Code;

            _groups.Add(body);

            while (!code.EndOfFile && !p.Code.PeekExact("}"))
            {
                var resetPos = code.Position;
                if (code.ReadExactWholeWord("before") || code.ReadExactWholeWord("after"))
                {
                    if (code.ReadExactWholeWord("group"))
                    {
                        code.Position = resetPos;
                        return(true);
                    }
                    code.Position = resetPos;
                }
                else if (code.ReadExactWholeWord("for"))
                {
                    if (code.ReadExactWholeWord("each"))
                    {
                        code.Position = resetPos;
                        return(true);
                    }
                    code.Position = resetPos;
                }
                else if (code.ReadExactWholeWord("default"))
                {
                    code.Position = resetPos;
                    return(true);
                }

                var stmt = Statement.Read(p);
                if (stmt == null)
                {
                    break;
                }
                body.stmts.Add(stmt);
            }

            return(true);
        }
Пример #13
0
        private Node ReadNestable(ReadParams p, DataType refDataType, Span openSpan, string text, string[] stopStrings)
        {
            GroupNode groupNode;
            string    endText;

            switch (text)
            {
            case "(":
                groupNode = new BracketsNode(p.Statement, openSpan);
                endText   = ")";
                break;

            default:
                throw new ArgumentOutOfRangeException("text");
            }

            if (stopStrings == null)
            {
                stopStrings = new string[] { endText }
            }
            ;
            else
            {
                stopStrings = stopStrings.Concat(new string[] { endText }).ToArray();
            }

            while (!p.Code.EndOfFile)
            {
                if (p.Code.ReadExact(endText))
                {
                    groupNode.Span = groupNode.Span.Envelope(p.Code.Span);
                    break;
                }

                var exp = ExpressionNode.Read(p, refDataType, stopStrings);
                if (exp == null)
                {
                    break;
                }
                groupNode.AddChild(exp);
            }

            return(groupNode);
        }
Пример #14
0
        public static ConditionalNode Read(ReadParams p, DataType refDataType, Span opSpan, string[] stopStrings)
        {
            var code = p.Code;
            var ret  = new ConditionalNode(p.Statement, opSpan);

            var condStopStrings = stopStrings == null || stopStrings.Length == 0 ? s_stopStrings : stopStrings.Concat(s_stopStrings).ToArray();
            var trueExp         = ExpressionNode.Read(p, refDataType, condStopStrings);

            if (trueExp == null)
            {
                p.Statement.ReportError(new Span(code.Position, code.Position + 1), CAError.CA0042);                    // Expected value to follow conditional '?'.
                return(ret);
            }
            ret._trueExp = trueExp;

            if (!code.ReadExact(':'))
            {
                p.Statement.ReportError(new Span(code.Position, code.Position + 1), CAError.CA0041);                    // Expected ':' to follow conditional result.
                return(ret);
            }

            var falseExp = ExpressionNode.Read(p, refDataType, condStopStrings);

            if (falseExp == null)
            {
                p.Statement.ReportError(new Span(code.Position, code.Position + 1), CAError.CA0043);                    // Expected value to follow conditional ':'.
                return(ret);
            }

            if (code.ReadExact('?'))
            {
                // Stacked conditional
                var group = new GroupNode(p.Statement, null);
                group.AddChild(falseExp);
                group.AddChild(ConditionalNode.Read(p, refDataType, code.Span, stopStrings));
                ret._falseExp = group;
            }
            else
            {
                ret._falseExp = falseExp;
            }

            return(ret);
        }
Пример #15
0
		public HeaderStatement(ReadParams p, Span keywordSpan)
			: base(p.CodeAnalyzer, keywordSpan)
		{
			p = p.Clone(this);
			var code = p.Code;

			if (!code.ReadExact('{'))
			{
				ReportError(keywordSpan, CAError.CA0019);	// Expected '{'.
				return;
			}

			while (!code.EndOfFile && !code.ReadExact("}"))
			{
				var stmt = Statement.Read(p);
				if (stmt == null) break;
				_body.Add(stmt);
			}
		}
Пример #16
0
    public HResult BeginRead(
        IntPtr pb, int cb, IMFAsyncCallback pCallback, object pUnkState)
    {
        // For reasons beyond my understanding, sometime we are asked to
        // return bytes at negative offsets.  In these cases, we change
        // the number of bytes requested to 0, which results in EndRead
        // returning 0 bytes.
        if (m_Position < 0)
        {
            cb = 0;
        }

        // This handles the case where SetCurrentPosition was called to
        // rewind the stream.
        if (m_Position == 0)
        {
            m_PositionReal = 0;
            m_SinceLast    = 0;
        }

        // Since BeginRead is async, caller could call GetCurrentPosition
        // while the read is running.  Spec says they get the new value.
        m_Position += cb;

        IMFAsyncResult pResult;
        ReadParams     rp = new ReadParams(pb, cb);

        HResult hr = MFExtern.MFCreateAsyncResult(
            rp, pCallback, pUnkState, out pResult);

        // If 2 BeginReads are called consecutively, might the Workqueue
        // run them concurrently?  I don't know, but that would be bad.
        if (MFError.Succeeded(hr))
        {
            hr = MFExtern.MFPutWorkItem(
                (int)MFASYNC_WORKQUEUE_TYPE.StandardWorkqueue,
                this, pResult);
        }

        return(hr);
    }
Пример #17
0
        public ReturnStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);

            var retDataType = p.FuncDef.DataType;

            if (retDataType != null && !retDataType.IsVoid)
            {
                _exp = ExpressionNode.Read(p, retDataType);
                if (_exp == null)
                {
                    ReportError(keywordSpan, CAError.CA0014);                           // Expected value after 'return'.
                }
            }

            if (!p.Code.ReadExact(';'))
            {
                ReportError(p.Code.Span, CAError.CA0015);                                       // Expected ';'.
            }
        }
Пример #18
0
        private IdentifierNode TryReadSubscript(ReadParams p, Span nameSpan, string name, Definition def)
        {
            if (def.DataType == null || def.DataType.AllowsSubscript == false)
            {
                return(new IdentifierNode(p.Statement, nameSpan, name, def));
            }

            var code     = p.Code;
            var resetPos = code.Position;

            if (code.ReadExact('['))
            {
                var exp1 = ExpressionNode.Read(p, null, "]", ",");
                if (exp1 != null)
                {
                    if (code.ReadExact(','))
                    {
                        var exp2 = ExpressionNode.Read(p, null, "]", ",");
                        if (exp2 != null)
                        {
                            if (code.ReadExact(']'))
                            {
                                return(new IdentifierNode(p.Statement, nameSpan, name, def,
                                                          subscriptAccessExps: new ExpressionNode[] { exp1, exp2 }));
                            }
                        }
                    }
                    else if (code.ReadExact(']'))
                    {
                        return(new IdentifierNode(p.Statement, nameSpan, name, def,
                                                  subscriptAccessExps: new ExpressionNode[] { exp1 }));
                    }
                }
            }

            // No match; reset back to before the array accessors started
            code.Position = resetPos;

            return(new IdentifierNode(p.Statement, nameSpan, name, def));
        }
Пример #19
0
    public HResult EndRead(IMFAsyncResult pResult, out int pcbRead)
    {
        // In theory, pcbRead might be less than what was requested. However
        // that seems to drive our caller nuts, so we only do that when
        // m_Position < 0.
        HResult hr;
        object  o;

        hr = pResult.GetObject(out o);

        if (MFError.Succeeded(hr))
        {
            ReadParams rp = (ReadParams)o;
            pcbRead = rp.cb;
        }
        else
        {
            pcbRead = 0;
        }

        return(hr);
    }
Пример #20
0
        public ForStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);
            var code = p.Code;

            if (!code.ReadExact('('))
            {
                ReportError(keywordSpan, CAError.CA0025);                       // Expected '('.
                return;
            }
            var errSpan = code.Span;

            _initExp = ExpressionNode.Read(p, null, ")");
            if (_initExp != null)
            {
                errSpan = _initExp.Span;
            }

            if (!code.ReadExact(';'))
            {
                ReportError(errSpan, CAError.CA0026);                   // Expected ';'.
                return;
            }
            errSpan = code.Span;

            _condExp = ExpressionNode.Read(p, null, ")");
            if (_condExp != null)
            {
                errSpan = _condExp.Span;
            }

            if (!code.ReadExact(';'))
            {
                ReportError(errSpan, CAError.CA0026);                   // Expected ';'.
                return;
            }
            errSpan = code.Span;

            _incExp = ExpressionNode.Read(p, null, ")");
            if (_incExp != null)
            {
                errSpan = _incExp.Span;
            }

            if (!code.ReadExact(')'))
            {
                ReportError(errSpan, CAError.CA0027);                   // Expected ')'.
                return;
            }
            errSpan = code.Span;

            if (!code.ReadExact('{'))
            {
                ReportError(errSpan, CAError.CA0019);                   // Expected '{'.
                return;
            }
            errSpan = code.Span;

            while (!code.EndOfFile && !code.ReadExact("}"))
            {
                var stmt = Statement.Read(p);
                if (stmt == null)
                {
                    break;
                }
                _body.Add(stmt);
                errSpan = stmt.Span;
            }
        }
Пример #21
0
        public SwitchStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);
            var code = p.Code;

            _condExp = ExpressionNode.Read(p, null);
            if (_condExp == null)
            {
                ReportError(keywordSpan, CAError.CA0018, "switch");                     // Expected condition after '{0}'.
                return;
            }
            var errorSpan = keywordSpan;

            if (_condExp.NumChildren > 1 && _condExp.LastChild is OperatorNode)
            {
                _condExp.RemoveChild(_compareOp = _condExp.LastChild as OperatorNode);
                errorSpan = _compareOp.Span;
            }

            if (!code.ReadExact('{'))
            {
                ReportError(errorSpan, CAError.CA0019);                 // Expected '{'.
                return;
            }
            errorSpan = code.Span;

            var insideDefault = false;

            while (!code.EndOfFile)
            {
                if (code.ReadExact('}'))
                {
                    break;
                }
                if (code.ReadExactWholeWord("case"))
                {
                    errorSpan = code.Span;

                    if (_cases.Count > 0)
                    {
                        var lastCase = _cases[_cases.Count - 1];
                        if (lastCase != null && lastCase.body.Count == 0)
                        {
                            lastCase.safeFallThrough = true;
                        }
                    }

                    _cases.Add(new Case
                    {
                        caseSpan = code.Span
                    });
                    insideDefault = false;

                    var exp = ExpressionNode.Read(p, _condExp.DataType, ":");
                    if (exp == null)
                    {
                        ReportError(errorSpan, CAError.CA0028);                                         // Expected case value.
                    }
                    else
                    {
                        _cases.Last().exp = exp;
                        errorSpan = exp.Span;
                    }

                    if (!code.ReadExact(':'))
                    {
                        ReportError(errorSpan, CAError.CA0029);                                                 // Expected ':'.
                    }
                }
                else if (code.ReadExactWholeWord("default"))
                {
                    if (_default != null)
                    {
                        ReportError(code.Span, CAError.CA0032);                         // Duplicate default case.
                    }

                    insideDefault = true;
                    _default      = new List <Statement>();

                    if (!code.ReadExact(':'))
                    {
                        ReportError(errorSpan, CAError.CA0029);                                                 // Expected ':'.
                    }
                }
                else if (code.ReadExactWholeWord("break"))
                {
                    if (insideDefault)
                    {
                        _default.Add(new BreakStatement(p, code.Span));
                    }
                    else if (_cases.Any())
                    {
                        _cases.Last().body.Add(new BreakStatement(p, code.Span));
                    }
                    else
                    {
                        ReportError(code.Span, CAError.CA0023);                         // 'break' is not valid here.
                    }
                }
                else
                {
                    var stmt = Statement.Read(p);
                    if (stmt == null)
                    {
                        break;
                    }
                    if (insideDefault)
                    {
                        _default.Add(stmt);
                    }
                    else if (_cases.Any())
                    {
                        _cases.Last().body.Add(stmt);
                    }
                    else
                    {
                        ReportError(stmt.Span, CAError.CA0030);                         // Statement is not valid here.
                    }
                }
            }
        }
Пример #22
0
        public SelectStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);
            var code = p.Code;

            code.ReadStringLiteral();

            if (!code.ReadExact('*'))
            {
                ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, "*");                   // Expected '{0}'.
                return;
            }

            var bodyStarted = false;

            while (!code.EndOfFile && !bodyStarted)
            {
                if (code.ReadExact('{'))
                {
                    bodyStarted = true;
                    break;
                }

                #region from
                if (code.ReadExact("from"))
                {
                    while (!code.EndOfFile)
                    {
                        if (code.ReadExact('{'))
                        {
                            bodyStarted = true;
                            break;
                        }

                        if (code.ReadWord())
                        {
                            if (code.Text == "where" || code.Text == "order")
                            {
                                code.Position = code.TokenStartPostion;
                                break;
                            }

                            if (code.Text == "of")
                            {
                                if (!code.ReadWord())
                                {
                                    ReportError(new Span(code.Position, code.Position + 1), CAError.CA0036);                                            // Expected table name after 'of'.
                                    return;
                                }
                            }

                            var def = (from d in CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetGlobalFromAnywhere(code.Text)
                                       where d is TableDefinition || d is RelIndDefinition || d is ExtractTableDefinition
                                       select d).FirstOrDefault();
                            if (def == null)
                            {
                                ReportError(code.Span, CAError.CA0035, code.Text);                                      // Table or relationship '{0}' does not exist.
                                return;
                            }
                            else
                            {
                                _tables.Add(def);
                            }
                        }
                        else if (code.ReadExact(','))
                        {
                        }
                        else
                        {
                            ReportError(code.Span, CAError.CA0034, "{");                                // Expected '{0}'.
                            return;
                        }
                    }
                }
                #endregion
                #region where
                else if (code.ReadExact("where"))
                {
                    _whereExp = ExpressionNode.Read(p, null, "from", "where", "order");

                    if (code.ReadExact(';'))
                    {
                        bodyStarted = true;
                        break;
                    }
                }
                #endregion
                #region order by
                else if (code.ReadExact("order"))
                {
                    if (!code.ReadExactWholeWord("by"))
                    {
                        ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, "by");                          // Expected '{0}'.
                        return;
                    }

                    while (!code.EndOfFile)
                    {
                        if (code.ReadExact('{'))
                        {
                            bodyStarted = true;
                            break;
                        }

                        if (code.ReadExact(','))
                        {
                            continue;
                        }

                        if (code.ReadExactWholeWord("from") || code.ReadExactWholeWord("where"))
                        {
                            code.Position = code.TokenStartPostion;
                            break;
                        }

                        if (!ReadTableColOrRelInd(p, true))
                        {
                            return;
                        }
                        if (!code.ReadExactWholeWord("asc"))
                        {
                            code.ReadExactWholeWord("desc");
                        }
                    }
                }
                #endregion
            }

            while (!code.EndOfFile && !code.ReadExact('}'))
            {
                if (code.ReadExactWholeWord("before") || code.ReadExactWholeWord("after"))
                {
                    if (!code.ReadExactWholeWord("group"))
                    {
                        ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, "group");                               // Expected '{0}'.
                        return;
                    }

                    if (code.ReadExactWholeWord("all"))
                    {
                    }
                    else if (!ReadTableColOrRelInd(p, false))
                    {
                        return;
                    }

                    if (!code.ReadExact(':'))
                    {
                        ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, ":");                           // Expected '{0}'.
                        return;
                    }

                    if (!ReadGroupStatements(p, false))
                    {
                        return;
                    }
                }
                else if (code.ReadExactWholeWord("for"))
                {
                    if (!code.ReadExactWholeWord("each"))
                    {
                        ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, "each");                                // Expected '{0}'.
                        return;
                    }

                    if (!code.ReadExact(':'))
                    {
                        ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, ":");                           // Expected '{0}'.
                        return;
                    }

                    ReadGroupStatements(p, false);
                }
                else if (code.ReadExactWholeWord("default"))
                {
                    if (!code.ReadExact(':'))
                    {
                        ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, ":");                           // Expected '{0}'.
                        return;
                    }

                    ReadGroupStatements(p, true);
                }
                else
                {
                    break;
                }
            }
        }
Пример #23
0
        public static Statement Read(ReadParams p)
        {
            p.Code.SkipWhiteSpace();
            if (p.Code.EndOfFile)
            {
                return(null);
            }

            var word = p.Code.PeekWordR();

            if (!string.IsNullOrEmpty(word))
            {
                switch (word)
                {
                case "break":
                    return(new BreakStatement(p, p.Code.MovePeekedSpan()));

                case "center":
                    return(new CenterStatement(p, p.Code.MovePeekedSpan()));

                case "col":
                case "colff":
                    return(new ColStatement(p, p.Code.MovePeekedSpan()));

                case "continue":
                    return(new ContinueStatement(p, p.Code.MovePeekedSpan()));

                case "extract":
                    return(new ExtractStatement(p, p.Code.MovePeekedSpan()));

                case "footer":
                    return(new FooterStatement(p, p.Code.MovePeekedSpan()));

                case "for":
                    return(new ForStatement(p, p.Code.MovePeekedSpan()));

                case "format":
                    return(new FormatStatement(p, p.Code.MovePeekedSpan()));

                case "header":
                    return(new HeaderStatement(p, p.Code.MovePeekedSpan()));

                case "if":
                    return(new IfStatement(p, p.Code.MovePeekedSpan()));

                case "page":
                    return(new PageStatement(p, p.Code.MovePeekedSpan()));

                case "return":
                    return(new ReturnStatement(p, p.Code.MovePeekedSpan()));

                case "row":
                    return(new RowStatement(p, p.Code.MovePeekedSpan()));

                case "select":
                    return(new SelectStatement(p, p.Code.MovePeekedSpan()));

                case "switch":
                    return(new SwitchStatement(p, p.Code.MovePeekedSpan()));

                case "while":
                    return(new WhileStatement(p, p.Code.MovePeekedSpan()));
                }
            }

            var stmt = new SimpleStatement(p.CodeAnalyzer);

            p = p.Clone(stmt);

            while (!p.Code.EndOfFile)
            {
                if (p.Code.ReadExact(';'))
                {
                    return(stmt);
                }

                var node = ExpressionNode.Read(p, null);
                if (node == null)
                {
                    break;
                }
                stmt.AddNode(node);
            }

            if (stmt.NumChildren == 0)
            {
                return(null);
            }
            return(stmt);
        }
Пример #24
0
        private Node ReadWord(ReadParams p, DataType refDataType)
        {
            var code     = p.Code;
            var word     = code.Text;
            var wordSpan = code.Span;

            if (code.ReadExact('('))
            {
                // This is a function call

                switch (word)
                {
                case "avg":
                case "count":
                case "sum":
                case "max":
                case "min":
                    return(AggregateFunctionCallNode.Read(p, wordSpan, word));
                }

                return(FunctionCallNode.Read(p, wordSpan, word));
            }

            if (code.ReadExact('.'))
            {
                var dotSpan = code.Span;

                if (code.ReadWord())
                {
                    var childWord    = code.Text;
                    var combinedWord = string.Concat(word, ".", childWord);
                    var combinedSpan = wordSpan.Envelope(code.Span);

                    if (code.ReadExact('('))
                    {
                        foreach (var parentDef in (from d in p.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetAny(code.Position + p.FuncOffset, word)
                                                   where d.AllowsChild
                                                   select d))
                        {
                            var childDef = parentDef.ChildDefinitions.FirstOrDefault(c => c.Name == childWord && c.ArgumentsRequired);
                            if (childDef != null)
                            {
                                return(FunctionCallNode.Read(p, combinedSpan, combinedWord, childDef));
                            }
                        }

                        ReportError(combinedSpan, CAError.CA0003, combinedWord);                                // Function '{0}' not found.
                        return(new UnknownNode(p.Statement, combinedSpan, combinedWord));
                    }
                    else                     // No opening bracket
                    {
                        foreach (var parentDef in (from d in p.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetAny(code.Position + p.FuncOffset, word)
                                                   where d.AllowsChild
                                                   select d))
                        {
                            var childDef = parentDef.ChildDefinitions.FirstOrDefault(c => c.Name == childWord && !c.ArgumentsRequired);
                            if (childDef != null)
                            {
                                return(TryReadSubscript(p, combinedSpan, combinedWord, childDef));
                            }
                        }

                        ReportError(combinedSpan, CAError.CA0001, combinedWord);                                // Unknown '{0}'.
                        return(new UnknownNode(p.Statement, combinedSpan, combinedWord));
                    }
                }
                else                                      // No word after dot
                {
                    ReportError(dotSpan, CAError.CA0004); // Expected identifier to follow '.'
                    return(new UnknownNode(p.Statement, wordSpan.Envelope(dotSpan), string.Concat(word, ".")));
                }
            }

            // Try to read array accessor
            if (code.PeekExact('['))
            {
                // Read a list of array accessors with a single expression
                var arrayResetPos     = code.TokenStartPostion;
                var arrayExps         = new List <ExpressionNode[]>();
                var lastArrayStartPos = code.Position;
                while (!code.EndOfFile)
                {
                    lastArrayStartPos = code.Position;
                    if (code.ReadExact('['))
                    {
                        var exp1 = ExpressionNode.Read(p, null, "]", ",");
                        if (exp1 != null)
                        {
                            if (code.ReadExact(']'))
                            {
                                // Brackets with single expression
                                arrayExps.Add(new ExpressionNode[] { exp1 });
                            }
                            else if (code.ReadExact(','))
                            {
                                var exp2 = ExpressionNode.Read(p, null, "]");
                                if (exp2 != null)
                                {
                                    if (code.ReadExact(']'))
                                    {
                                        arrayExps.Add(new ExpressionNode[] { exp1, exp2 });
                                    }
                                    else
                                    {
                                        code.Position = lastArrayStartPos;
                                        break;
                                    }
                                }
                                else
                                {
                                    code.Position = lastArrayStartPos;
                                    break;
                                }
                            }
                            else
                            {
                                code.Position = lastArrayStartPos;
                                break;
                            }
                        }
                        else
                        {
                            code.Position = lastArrayStartPos;
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                var defs = p.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetAny(code.Position + p.FuncOffset, word).ToArray();

                // Try to match to a variable defined as an array
                if (arrayExps.Count > 0)
                {
                    // Check if it's a variable being accessed
                    foreach (var def in defs)
                    {
                        if (def is VariableDefinition)
                        {
                            var vardef       = def as VariableDefinition;
                            var arrayLengths = vardef.ArrayLengths;
                            if (arrayLengths == null)
                            {
                                continue;
                            }

                            if (arrayLengths.Length == arrayExps.Count && arrayExps.All(x => x.Length == 1))
                            {
                                return(new IdentifierNode(p.Statement, wordSpan, word, def, (from e in arrayExps select e[0])));
                            }
                            else if (arrayLengths.Length == arrayExps.Count - 1 &&
                                     vardef.DataType != null &&
                                     vardef.DataType.AllowsSubscript &&
                                     arrayExps.Take(arrayLengths.Length).All(x => x.Length == 1))
                            {
                                // Last array accessor is a string subscript
                                return(new IdentifierNode(p.Statement, wordSpan, word, def,
                                                          (from e in arrayExps.Take(arrayExps.Count - 1) select e[0]),
                                                          arrayExps.Last()));
                            }
                        }
                    }
                }

                // Try to match to a string that allows a subscript with 1 or 2 arguments
                code.Position = arrayResetPos;
                var subDef = (from d in defs where d.DataType != null && d.DataType.AllowsSubscript select d).FirstOrDefault();
                if (subDef != null)
                {
                    return(TryReadSubscript(p, wordSpan, word, subDef));
                }
            }

            if (refDataType != null)
            {
                if (refDataType.HasCompletionOptions)
                {
                    var enumOptDef = refDataType.GetEnumOption(word);
                    if (enumOptDef != null)
                    {
                        return(new IdentifierNode(p.Statement, wordSpan, word, enumOptDef));
                    }
                }

                switch (refDataType.ValueType)
                {
                case ValType.Table:
                {
                    var table = DkDict.Dict.GetTable(word);
                    if (table != null)
                    {
                        return(new IdentifierNode(p.Statement, wordSpan, word, table.Definition));
                    }

                    var indrel = DkDict.Dict.GetRelInd(word);
                    if (indrel != null)
                    {
                        return(new IdentifierNode(p.Statement, wordSpan, word, indrel.Definition));
                    }
                }
                break;

                case ValType.IndRel:
                {
                    var indrel = DkDict.Dict.GetRelInd(word);
                    if (indrel != null)
                    {
                        return(new IdentifierNode(p.Statement, wordSpan, word, indrel.Definition));
                    }
                }
                break;
                }
            }

            var wordDefs = (from d in p.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetAny(code.Position + p.FuncOffset, word)
                            where !d.RequiresChild && !d.ArgumentsRequired && !d.RequiresRefDataType
                            orderby d.SelectionOrder descending
                            select d).ToArray();

            if (wordDefs.Length > 0)
            {
                return(new IdentifierNode(p.Statement, wordSpan, word, wordDefs[0]));
            }

            return(new UnknownNode(p.Statement, wordSpan, word));

            // Single word. Don't attempt to find the definition now because it could be an enum option.
            //return new IdentifierNode(p.Statement, wordSpan, word, null);
        }
Пример #25
0
 public static ExpressionNode Read(ReadParams p, DataType refDataType, params string[] stopStrings)
 {
     return(Read(p, refDataType, false, stopStrings));
 }
Пример #26
0
        public static ExpressionNode Read(ReadParams p, DataType refDataType, bool stayOnSameLine, params string[] stopStrings)
        {
            ExpressionNode exp           = null;
            var            code          = p.Code;
            var            lastPos       = code.Position;
            var            parseDataType = refDataType;

            while (!code.EndOfFile)
            {
                switch (code.PeekChar())
                {
                case ';':
                case '{':
                case '}':
                    return(exp);
                }

                if (stopStrings != null)
                {
                    foreach (var str in stopStrings)
                    {
                        if (str.IsWord())
                        {
                            if (code.PeekExactWholeWord(str))
                            {
                                return(exp);
                            }
                        }
                        else
                        {
                            if (code.PeekExact(str))
                            {
                                return(exp);
                            }
                        }
                    }
                }

                if (!code.Read())
                {
                    break;
                }

                if (stayOnSameLine)
                {
                    if (code.PositionsAreOnDifferentLines(lastPos, code.TokenStartPostion))
                    {
                        code.Position = code.TokenStartPostion;
                        break;
                    }
                    lastPos = code.Position;
                }

                if (exp == null)
                {
                    exp = new ExpressionNode(p.Statement);
                }

                switch (code.Type)
                {
                case CodeType.Number:
                    exp.AddChild(new NumberNode(p.Statement, code.Span, code.Text));
                    break;

                case CodeType.StringLiteral:
                    if (code.Text.StartsWith("'"))
                    {
                        exp.AddChild(new CharLiteralNode(p.Statement, code.Span, CodeParser.StringLiteralToString(code.Text)));
                    }
                    else
                    {
                        exp.AddChild(new StringLiteralNode(p.Statement, code.Span, CodeParser.StringLiteralToString(code.Text)));
                    }
                    break;

                case CodeType.Word:
                    exp.AddChild(exp.ReadWord(p, parseDataType));
                    break;

                case CodeType.Operator:
                    switch (code.Text)
                    {
                    case "(":
                    {
                        var opText    = code.Text;
                        var startPos  = code.Span.Start;
                        var resumePos = code.Position;
                        var dataType  = DataType.TryParse(new DataType.ParseArgs
                            {
                                Code             = code,
                                Flags            = DataType.ParseFlag.Strict,
                                DataTypeCallback = name =>
                                {
                                    return(p.Statement.CodeAnalyzer.PreprocessorModel.DefinitionProvider.
                                           GetAny <DataTypeDefinition>(startPos + p.FuncOffset, name).FirstOrDefault());
                                },
                                VariableCallback = name =>
                                {
                                    return(p.Statement.CodeAnalyzer.PreprocessorModel.DefinitionProvider.
                                           GetAny <VariableDefinition>(startPos + p.FuncOffset, name).FirstOrDefault());
                                },
                                TableFieldCallback = (tableName, fieldName) =>
                                {
                                    foreach (var tableDef in p.Statement.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetGlobalFromFile(tableName))
                                    {
                                        if (tableDef.AllowsChild)
                                        {
                                            foreach (var fieldDef in tableDef.GetChildDefinitions(fieldName))
                                            {
                                                return(new Definition[] { tableDef, fieldDef });
                                            }
                                        }
                                    }

                                    return(null);
                                },
                                VisibleModel = false
                            });
                        if (dataType != null && code.ReadExact(')'))
                        {
                            // This is a cast
                            var span = new Span(startPos, code.Span.End);
                            exp.AddChild(new CastNode(p.Statement, span, dataType, ExpressionNode.Read(p, dataType, stayOnSameLine, stopStrings)));
                        }
                        else
                        {
                            code.Position = resumePos;
                            exp.AddChild(exp.ReadNestable(p, parseDataType, code.Span, opText, null));
                        }
                    }
                    break;

                    //case "[":
                    //	exp.AddChild(exp.ReadNestable(p, code.Span, code.Text, stopStrings));
                    //	break;
                    case "-":
                    {
                        var lastNode = exp.LastChild;
                        if (lastNode == null || lastNode is OperatorNode)
                        {
                            exp.AddChild(new OperatorNode(p.Statement, code.Span, code.Text, SpecialOperator.UnaryMinus));
                        }
                        else
                        {
                            exp.AddChild(new OperatorNode(p.Statement, code.Span, code.Text, null));
                        }
                    }
                    break;

                    case "?":
                        parseDataType = refDataType;
                        exp.AddChild(ConditionalNode.Read(p, parseDataType, code.Span, stopStrings));
                        break;

                    case "=":
                    {
                        var rDataType = exp.NumChildren > 0 ? exp.LastChild.DataType : null;
                        exp.AddChild(new OperatorNode(p.Statement, code.Span, code.Text, null));
                        exp.AddChild(ExpressionNode.Read(p, rDataType, stopStrings));
                    }
                    break;

                    case "==":
                    case "!=":
                    case "<":
                    case "<=":
                    case ">":
                    case ">=":
                    {
                        if (exp.NumChildren > 0)
                        {
                            var dt = exp.LastChild.DataType;
                            if (dt != null)
                            {
                                parseDataType = dt;
                            }
                        }
                        exp.AddChild(new OperatorNode(p.Statement, code.Span, code.Text, null));
                    }
                    break;

                    default:
                        exp.AddChild(new OperatorNode(p.Statement, code.Span, code.Text, null));
                        break;
                    }
                    break;

                default:
                    exp.ReportError(code.Span, CAError.CA0001, code.Text);                              // Unknown '{0}'.
                    exp.AddChild(new UnknownNode(p.Statement, code.Span, code.Text));
                    break;
                }
            }

            return(exp);
        }
Пример #27
0
        public FormatStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);
            var code      = p.Code;
            var errorSpan = keywordSpan;

            while (!code.EndOfFile)
            {
                if (code.ReadExact(';'))
                {
                    break;
                }

                switch (code.PeekWordR())
                {
                case "rows":
                    errorSpan = code.MovePeekedSpan();
                    if (!code.ReadExact('='))
                    {
                        ReportError(errorSpan, CAError.CA0033);                                 // Expected '='.
                        return;
                    }
                    else
                    {
                        errorSpan = code.Span;
                    }

                    _rowsExp = ExpressionNode.Read(p, null, "rows", "cols", "genpages", "outfile");
                    if (_rowsExp != null)
                    {
                        errorSpan = _rowsExp.Span;
                    }
                    break;

                case "cols":
                    errorSpan = code.MovePeekedSpan();
                    if (!code.ReadExact('='))
                    {
                        ReportError(errorSpan, CAError.CA0033);                                 // Expected '='.
                        return;
                    }
                    else
                    {
                        errorSpan = code.Span;
                    }

                    _colsExp = ExpressionNode.Read(p, null, "rows", "cols", "genpages", "outfile");
                    if (_colsExp != null)
                    {
                        errorSpan = _colsExp.Span;
                    }
                    break;

                case "genpages":
                    errorSpan = code.MovePeekedSpan();
                    if (!code.ReadExact('='))
                    {
                        ReportError(errorSpan, CAError.CA0033);                                 // Expected '='.
                        return;
                    }
                    else
                    {
                        errorSpan = code.Span;
                    }

                    _genpagesExp = ExpressionNode.Read(p, null, "rows", "cols", "genpages", "outfile");
                    if (_genpagesExp != null)
                    {
                        errorSpan = _genpagesExp.Span;
                    }
                    break;

                case "outfile":
                    errorSpan = code.MovePeekedSpan();
                    if (!code.ReadExact('='))
                    {
                        ReportError(errorSpan, CAError.CA0033);                                 // Expected '='.
                        return;
                    }
                    else
                    {
                        errorSpan = code.Span;
                    }

                    _outfileExp = ExpressionNode.Read(p, null, "rows", "cols", "genpages", "outfile");
                    if (_outfileExp != null)
                    {
                        errorSpan = _outfileExp.Span;
                    }
                    break;

                default:
                    ReportError(errorSpan, CAError.CA0015);                             // Expected ';'.
                    return;
                }
            }
        }
Пример #28
0
        public static AggregateFunctionCallNode Read(ReadParams p, Span funcNameSpan, string funcName)
        {
            var ret  = new AggregateFunctionCallNode(p.Statement, funcNameSpan, funcName);
            var code = p.Code;

            if (funcName == "count")
            {
                if (!code.ReadExact('*'))
                {
                    ret.ReportError(CAError.CA0060);                            // Expected '*' in count().
                    return(ret);
                }
            }
            else
            {
                var exp = ExpressionNode.Read(p, null, ",", ")");
                if (exp == null)
                {
                    ret.ReportError(CAError.CA0061);                            // Expected aggregate expression.
                    return(ret);
                }
                ret._aggExp = exp;
            }

            while (!code.EndOfFile)
            {
                if (code.ReadExact(')'))
                {
                    break;
                }

                if (code.ReadExact(','))
                {
                    if (code.ReadExactWholeWord("where"))
                    {
                        var exp = ExpressionNode.Read(p, null, ",", ")");
                        if (exp == null)
                        {
                            ret.ReportError(code.Span, CAError.CA0062, "where");                                // Expected expression to follow '{0}'.
                            return(ret);
                        }
                        ret._whereExp = exp;
                    }
                    else if (code.ReadExactWholeWord("group"))
                    {
                        var startPos = code.Position;
                        if (code.ReadWord())
                        {
                            var tableDef = (from d in p.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetGlobalFromAnywhere(code.Text)
                                            where d is TableDefinition || d is ExtractTableDefinition
                                            select d).FirstOrDefault();
                            if (tableDef == null)
                            {
                                ret.ReportError(code.Span, CAError.CA0064, code.Text);                                  // Table '{0}' does not exist.
                                return(ret);
                            }

                            if (!code.ReadExact('.'))
                            {
                                ret.ReportError(code.Span, CAError.CA0066);                                     // Expected '.'
                                return(ret);
                            }

                            Definition fieldDef = null;
                            if (!code.ReadWord() || (fieldDef = tableDef.GetChildDefinitions(code.Text).FirstOrDefault()) == null)
                            {
                                ret.ReportError(code.Span, CAError.CA0067);                                 // Expected column name.
                                return(ret);
                            }
                        }
                        else
                        {
                            ret.ReportError(code.Span, CAError.CA0065);                                 // Expected table name to follow 'group'.
                            return(ret);
                        }
                    }
                    else if (code.ReadExactWholeWord("all"))
                    {
                        // Nothing follows 'all'
                    }
                    else if (code.ReadExactWholeWord("in"))
                    {
                        if (!code.ReadStringLiteral())
                        {
                            ret.ReportError(code.Span, CAError.CA0068);                                 // Expected select name to follow 'in'.
                            return(ret);
                        }
                    }
                }
                else
                {
                    ret.ReportError(CAError.CA0063);                            // Expected ')'.
                }
            }

            return(ret);
        }
Пример #29
0
        public IfStatement(ReadParams p, Span keywordSpan)
            : base(p.CodeAnalyzer, keywordSpan)
        {
            p = p.Clone(this);
            var code     = p.Code;
            var stmtSpan = keywordSpan;

            while (!code.EndOfFile)
            {
                var condition = ExpressionNode.Read(p, null);
                if (condition == null)
                {
                    ReportError(stmtSpan, CAError.CA0018, "if");                        // Expected condition after '{0}'.
                    break;
                }

                _conditions.Add(condition);

                if (!code.ReadExact("{"))
                {
                    ReportError(stmtSpan, CAError.CA0019);                      // Expected '{'.
                    break;
                }

                var trueBody = new List <Statement>();
                while (!code.EndOfFile && !code.ReadExact("}"))
                {
                    var stmt = Statement.Read(p);
                    if (stmt == null)
                    {
                        break;
                    }
                    trueBody.Add(stmt);
                }
                _trueBodies.Add(trueBody);

                if (code.ReadExactWholeWord("else"))
                {
                    stmtSpan = code.Span;

                    if (code.ReadExactWholeWord("if"))
                    {
                        stmtSpan = code.Span;
                        continue;
                    }

                    if (!code.ReadExact("{"))
                    {
                        ReportError(stmtSpan, CAError.CA0019);                          // Expected '{'.
                        break;
                    }

                    _falseBody = new List <Statement>();
                    while (!code.EndOfFile && !code.ReadExact("}"))
                    {
                        var stmt = Statement.Read(p);
                        if (stmt == null)
                        {
                            break;
                        }
                        _falseBody.Add(stmt);
                    }
                }

                break;
            }
        }
Пример #30
0
 public CenterStatement(ReadParams p, Span keywordSpan)
     : base(p.CodeAnalyzer, keywordSpan)
 {
     p.Code.ReadExact(';');              // Optional
 }