/// <summary> /// 创建CSV的AST树 /// </summary> /// <param name="parseTree">解析树</param> /// <returns>CSV结构</returns> private static CsvTable CreateCsvAST(string filePath, CsvParser.CsvTabContext parseTree) { var table = new CsvTable(); for (int i = 0; i < parseTree._rows.Count; i++) { var row = new CsvRow(); row.isEmpty = false; var emptyCount = 0; var cols = parseTree._rows[i]._cols; for (int j = 0; j < cols.Count + 1; j++) { var col = new CsvCol(); ParserRuleContext colField = null; CsvTxtContext txtField = null; if (j < cols.Count) { colField = cols[j]; txtField = cols[j].content; } else { colField = parseTree._rows[i].end; txtField = colField != null ? parseTree._rows[i].end.content : null; } if (txtField != null && txtField._txt.Count > 0) { IList <IToken> tokens = txtField._txt; StringBuilder sb = new StringBuilder(); foreach (var token in tokens) { sb.Append(token.Text); } col.text = sb.ToString(); col.row = tokens[0].Line; col.col = tokens[0].Column; col.start = tokens[0].StartIndex; col.stop = tokens[tokens.Count - 1].StopIndex; } else { col.text = string.Empty; col.row = colField.Start.Line; col.col = colField.Start.Column; col.start = colField.Start.StartIndex; col.stop = colField.Stop.StartIndex; } row.Add(col); if (string.IsNullOrEmpty(col.text)) { emptyCount++; } } row.isEmpty = emptyCount == row.Count; table.Add(row); } return(table); }