/// <summary> /// 合并多行注释 /// </summary> /// <param name="codeElement"></param> private void MergeMulitRem(AnalyzeElement codeElement) { var last = codeElement.Elements[codeElement.Elements.Count - 1]; codeElement.Elements.RemoveAt(codeElement.Elements.Count - 1); var element = codeElement.Elements[codeElement.Elements.Count - 1]; element.ItemRace = CodeItemRace.Assist; element.ItemFamily = CodeItemFamily.Rem; element.Word += last.Word; codeElement.CurrentElement = element; bool isShift = false; for (CurWordIndex++; CurWordIndex < WordElements.Count; CurWordIndex++) { WordElement curElement = WordElements[CurWordIndex]; element.Word += curElement.RealWord; if (isShift && curElement.Char == '/')//注释结束 { return; } isShift = curElement.Char == '*'; } }
///// <summary> ///// 组合代码 ///// </summary> ///// <param name="isCodeBlock"></param> ///// <param name="codeElement"></param> //void JoinCode(bool isCodeBlock, WordElement codeElement) //{ // if (contentElement != null) // { // if (isCodeBlock) // { // AppendContentCode(contentElement); // contentElement = null; // } // else // { // contentElement.Append(codeElement.Element.End, $"{{{codeElement.RealWord.Trim()}}}"); // } // } // else if (!isCodeBlock) // { // Codes.Add($"strResult.Append({codeElement.RealWord.Trim()});"); // } // if (isCodeBlock && codeElement != null) // { // Codes.Add(codeElement.RealWord); // } // if (CurWordIndex < this.WordElements.Count && this.WordElements[CurWordIndex].Element.IsSpace) // { // CurWordIndex--; //空白还回去 // } //} #endregion #region 特殊单元的组合 /// <summary> /// 处理当前语言内容 /// </summary> /// <param name="codeElement">语言节点</param> /// <param name="preChar"></param> public override void MergeLanWord(AnalyzeElement codeElement, ref char preChar) { WordElement cur = WordElements[CurWordIndex]; codeElement.Append(cur); if (!cur.IsPunctuate) { preChar = '\0'; return; } switch (cur.Char) { #region 注释 case '/': if (preChar != '/') { preChar = '/'; return; } MergeSinleRem(codeElement); break; case '*': if (preChar == '/') { MergeMulitRem(codeElement); } break; #endregion #region 字符串 case '@': preChar = '@'; return; case '\"': if (preChar == '@') { var block = codeElement.Elements[codeElement.Elements.Count - 2]; block.Append(cur); codeElement.Elements.RemoveAt(codeElement.Elements.Count - 1); MergeMulitString(block); } else { MergeString(cur, '\"'); } break; case '\'': MergeString(cur, '\''); break; #endregion } preChar = '\0'; }
///// <summary> ///// 合并普通文本内容块 ///// </summary> ///// <param name="curElement">要合并部分</param> //protected override void MergeContentElement(WordElement curElement) //{ // if (curElement == null) // return; // if (curElement.Element.IsPunctuate) // { // switch (curElement.Char) // { // case '{': // case '}': // case '\"': // curElement.Append(curElement.Element.End, curElement.Char); // break; // } // } // if (contentElement == null) // contentElement = curElement; // else // contentElement.Merge(curElement); //} // /// <summary> // /// 以当前语言的代码方式的普通文本内容拼接的代码 // /// </summary> // /// <param name="content">普通内容文本</param> // /// <returns></returns> // protected override string AppendContentCode(string content) // { // return $@"{CodeSpace}strResult.Append($@"" //{content}"");"; // } /// <summary> /// 检查标点打头的代码块 /// </summary> /// <returns></returns> protected override bool CheckPunctuateStartCode() { if (WordElements[CurWordIndex].Char == '}') { var codeElement = new AnalyzeElement(); return(CheckIsCodeBlockEnd(codeElement)); } return(base.CheckPunctuateStartCode()); }
/// <summary> /// 处理当前语言内容 /// </summary> /// <param name="codeElement">语言节点</param> /// <param name="preChar"></param> public override void MergeLanWord(AnalyzeElement codeElement, ref char preChar) { WordElement cur = WordElements[CurWordIndex]; codeElement.Append(cur); if (!cur.IsPunctuate) { preChar = '\0'; return; } switch (cur.Char) { #region 注释 case '-': if (preChar != '-') { preChar = '-'; return; } { var block = codeElement.Elements[codeElement.Elements.Count - 2]; block.Append(cur); codeElement.Elements.RemoveAt(codeElement.Elements.Count - 1); MergeLuaRem(block); block.SetRace(CodeItemRace.Assist, CodeItemFamily.Rem); } break; #endregion #region 字符串 case '\"': case '\'': MergeString(cur, cur.Char); break; case '[': if (preChar != '[') { preChar = '['; return; } { var block = codeElement.Elements[codeElement.Elements.Count - 2]; block.Append(cur); codeElement.Elements.RemoveAt(codeElement.Elements.Count - 1); MergeMulitString(block); block.SetRace(CodeItemRace.Value, CodeItemFamily.Constant, CodeItemType.String); } break; #endregion } preChar = '\0'; }
/// <summary> /// 检查单词打头的代码块 /// </summary> /// <returns></returns> protected override bool CheckWordStartCode() { WordElements[CurWordIndex - 1].SetRace(CodeItemRace.Assist, CodeItemFamily.Range); var codeElement = new AnalyzeElement(); switch (WordElements[CurWordIndex].RealWord) { case "end": //@end表示LUA范围结束 return(CheckIsCodeBlockEnd(codeElement)); case "if": case "elseif": codeElement.Append(WordElements[CurWordIndex]); //FindCouple(codeElement, '(', ')', true, true); //TryGoNextWithWord(codeElement, "then", true); StepEndWord(codeElement, "then"); CheckMulitCode(codeElement); break; case "for": codeElement.Append(WordElements[CurWordIndex]); StepEndWord(codeElement, "do"); CheckMulitCode(codeElement); break; case "while": codeElement.Append(WordElements[CurWordIndex]); //FindCouple(codeElement, '(', ')', true, true); //TryGoNextWithWord(codeElement, "go", true); StepEndWord(codeElement, "go"); CheckMulitCode(codeElement); break; case "else": case "repeat": codeElement.Append(WordElements[CurWordIndex]); CheckMulitCode(codeElement); break; case "until": codeElement.Append(WordElements[CurWordIndex]); FindCouple(codeElement, '(', ')', true, true); break; default: WordElements[CurWordIndex - 1].SetRace(CodeItemRace.Assist, CodeItemFamily.Sharp); FindVariable(codeElement, '.', ':'); codeElement.IsBlock = true; foreach (var item in codeElement.Elements) { item.Words.ForEach(p => p.IsBlock = true); } break; } JoinCode(codeElement); return(true); }
public List <AnalyzeElement> MergeElements() { foreach (var element in TemplateElements) { element.Elements.ForEach(p => p.Release()); } List <AnalyzeElement> elements = new List <AnalyzeElement>(); AnalyzeElement preValue = null; foreach (var element in TemplateElements) { if (element.Elements.Count == 0) { continue; } if (element.IsContent) { if (preValue != null) { element.Elements.ForEach(p => preValue.Append(p)); } else { elements.Add(element); preValue = element; } } else if (element.IsBlock) { if (preValue == null) { elements.Add(preValue = new AnalyzeElement { IsContent = true }); } preValue.Append("%s"); preValue.AddAssist(element); } else { preValue = null; elements.Add(element); } } return(elements); }
public List <AnalyzeElement> AllToCode() { var elements = MergeElements(); int idx = 0; List <AnalyzeElement> result = new List <AnalyzeElement>(); foreach (var element in elements) { if (!element.IsContent) { result.Add(element); continue; } string content = element.Content(); if (string.IsNullOrWhiteSpace(content)) { continue; } if (idx == 0) { content = content.TrimStart(); } var newElement = new AnalyzeElement(); newElement.Append($"str{++idx} = "); newElement.Append("[["); newElement.Append(content); newElement.Append("]]"); newElement.Append(CreateLineElement()); if (element.Assists.Count > 0) { var format = new StringBuilder(); format.Append($"str{++idx} = string.format(str{idx - 1}"); foreach (var arg in element.Assists) { format.AppendFormat(" , tostring({0})", arg.Code().Trim()); } format.Append(")"); newElement.Append(format.ToString()); newElement.Append(CreateLineElement()); } newElement.Append($"__code = __code .. str{idx}"); result.Add(newElement); } return(result); }
/// <summary> /// 合并注释 /// </summary> /// <param name="codeElement"></param> private void MergeSinleRem(AnalyzeElement codeElement) { var last = codeElement.Elements[codeElement.Elements.Count - 1]; codeElement.Elements.RemoveAt(codeElement.Elements.Count - 1); var element = codeElement.Elements[codeElement.Elements.Count - 1]; element.ItemRace = CodeItemRace.Assist; element.ItemFamily = CodeItemFamily.Rem; element.Word += last.Word; codeElement.CurrentElement = element; for (CurWordIndex++; CurWordIndex < WordElements.Count; CurWordIndex++) { if (WordElements[CurWordIndex].IsLine) //行已结束 { --CurWordIndex; //回退 return; } element.Word += WordElements[CurWordIndex].RealWord; } }
/// <summary> /// 检查单词打头的代码块 /// </summary> /// <returns></returns> protected override bool CheckWordStartCode() { var codeElement = new AnalyzeElement(); switch (WordElements[CurWordIndex].RealWord) { case "if": case "for": case "foreach": case "switch": codeElement.ItemRace = CodeItemRace.Range; codeElement.Append(WordElements[CurWordIndex]); FindCouple(codeElement, '(', ')', true, true); TryGoNextWithWord(codeElement, "{", true); CheckMulitCode(codeElement); break; case "else": //试图查找后续的if codeElement.ItemRace = CodeItemRace.Range; codeElement.Append(WordElements[CurWordIndex]); if (TryGoNextWithWord(codeElement, "if", true)) { FindCouple(codeElement, '(', ')', true, true); } TryGoNextWithWord(codeElement, "{", true); CheckMulitCode(codeElement); break; default: FindVariable(codeElement, '.'); break; } JoinCode(codeElement); return(true); }