public static for_语句 Parse(char[] chars, int beginIndex, int endIndex, out int 语句结束位置) { 语句结束位置 = -1; StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces); if (span.isEmpty) { return(null); } if (!Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "for")) { return(null); } int 左小括号位置 = StrUtil.FindForwardUntilNot(chars, span.iLeft + 3, span.iRight, Parser._whiteSpaces); if (左小括号位置 == -1) { throw new 语法错误_Exception("未结束的 for 语句 。", chars, span.iLeft + 1); } char c = chars[左小括号位置]; if (c != '(') { throw new 语法错误_Exception("for 语句 缺少 左小括号 。", chars, 左小括号位置); } int 右小括号位置 = Parser.Find_小括号_右(chars, 左小括号位置 + 1, span.iRight); if (右小括号位置 == -1) { throw new 语法错误_Exception("for 语句 缺少 右小括号 。", chars, 左小括号位置 + 1); } //StrSpan span判断 = StrUtil.Trim(chars, 左小括号位置 + 1, 右小括号位置 - 1, Parser._whiteSpaces); //if (span判断.isEmpty) // throw new 语法错误_Exception("for 语句 缺少表达式 。", chars, 左小括号位置); //表达式 for_表达式 = 表达式_Parser.Parse(chars, 左小括号位置 + 1, 右小括号位置 - 1); 表达式[] 小括号表达式s = Parse_小括号(chars, 左小括号位置, 右小括号位置); StrSpan span子句 = StrUtil.Trim(chars, 右小括号位置 + 1, span.iRight, Parser._whiteSpaces); if (span子句.isEmpty) { throw new 语法错误_Exception("for 语句 缺少子句 。", chars, 右小括号位置 + 1); } 块作用域 子句 = Parse_子句(chars, span子句.iLeft, span子句.iRight, out 语句结束位置); //j = span子句.iRight; return(new for_语句(小括号表达式s, 子句)); }
public static continue_语句 Parse_continue_语句(char[] chars, int beginIndex, int endIndex, out int 语句结束位置) { 语句结束位置 = -1; StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces); if (span.isEmpty) { return(null); } if (!Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "continue")) { return(null); } int break后的位置 = span.iLeft + 5; if (break后的位置 > span.iRight) { throw new InnerCException("未结束的 continue 语句 。", chars, span.iLeft); } char c = chars[break后的位置]; if (c == '_' || StrUtil.IsLetter(c) || StrUtil.IsNumber(c)) { return(null); } int break后不是空白的位置 = StrUtil.FindForwardUntilNot(chars, break后的位置, span.iRight, Parser._whiteSpaces); if (break后不是空白的位置 == -1) { throw new InnerCException("未结束的 continue 语句,缺少分号 \";\" 。", chars, span.iLeft); } c = chars[break后不是空白的位置]; if (c != ';') { throw new InnerCException("无效的 continue 语句,无效的字符 \"" + c + "\" 。", chars, break后不是空白的位置); } 语句结束位置 = break后不是空白的位置; return(new continue_语句()); }
public static while_语句 Parse(char[] chars, int beginIndex, int endIndex, out int 语句结束位置) { 语句结束位置 = -1; StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces); if (span.isEmpty) { return(null); } if (!Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "while")) { return(null); } int 左小括号位置 = StrUtil.FindForwardUntilNot(chars, span.iLeft + 5, span.iRight, Parser._whiteSpaces); if (左小括号位置 == -1) { throw new 语法错误_Exception("未结束的 while 语句 。", chars, span.iLeft + 1); } char c = chars[左小括号位置]; if (c != '(') { throw new 语法错误_Exception("while 语句 缺少条件判断 左小括号 。", chars, 左小括号位置); } int 右小括号位置 = Parser.Find_小括号_右(chars, 左小括号位置 + 1, span.iRight); if (右小括号位置 == -1) { throw new 语法错误_Exception("while 语句 缺少条件判断 右小括号 。", chars, 左小括号位置 + 1); } StrSpan span判断 = StrUtil.Trim(chars, 左小括号位置 + 1, 右小括号位置 - 1, Parser._whiteSpaces); if (span判断.isEmpty) { throw new 语法错误_Exception("while 语句 缺少判断表达式 。", chars, 左小括号位置); } 表达式 while_条件 = 表达式_Parser.Parse(chars, 左小括号位置 + 1, 右小括号位置 - 1); StrSpan span子句 = StrUtil.Trim(chars, 右小括号位置 + 1, span.iRight, Parser._whiteSpaces); if (span子句.isEmpty) { throw new 语法错误_Exception("while 语句 缺少子句 。", chars, 右小括号位置 + 1); } 块作用域 子句 = Parse_子句(chars, span子句.iLeft, span子句.iRight, out 语句结束位置); //j = span子句.iRight; return(new while_语句(while_条件, 子句)); }
private static if_分句 Parse_if_分句(char[] chars, int beginIndex, int endIndex, out int j) { j = -1; StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces); if (span.isEmpty) { return(null); } bool b = Parser.开头一段_Equals(chars, span.iLeft, span.iRight, "if"); if (!b) { return(null); } int 左小括号位置 = StrUtil.FindForwardUntilNot(chars, span.iLeft + 2, span.iRight, Parser._whiteSpaces); if (左小括号位置 == -1) { throw new InnerCException("未结束的 if 语句 。", chars, span.iLeft + 1); } char c = chars[左小括号位置]; if (c != '(') { throw new InnerCException("if 语句 缺少条件判断 左小括号 。", chars, 左小括号位置); } int 右小括号位置 = Parser.Find_小括号_右(chars, 左小括号位置 + 1, span.iRight); if (右小括号位置 == -1) { throw new InnerCException("if 语句 缺少条件判断 右小括号 。", chars, 左小括号位置 + 1); } StrSpan span判断 = StrUtil.Trim(chars, 左小括号位置 + 1, 右小括号位置 - 1, Parser._whiteSpaces); if (span判断.isEmpty) { throw new InnerCException("if 语句 缺少判断表达式 。", chars, 左小括号位置); } 表达式 if_判断 = 表达式_Parser.Parse(chars, 左小括号位置 + 1, 右小括号位置 - 1); StrSpan span子句 = StrUtil.Trim(chars, 右小括号位置 + 1, span.iRight, Parser._whiteSpaces); if (span子句.isEmpty) { throw new InnerCException("if 语句 缺少子句 。", chars, 右小括号位置 + 1); } 块作用域 子句 = Parse_子句(chars, span子句.iLeft, span子句.iRight, out j); //j = span子句.iRight; return(new if_分句(if_判断, 子句)); }
public static 变量声明和初始化 Parse_变量声明(char[] chars, int beginIndex, int endIndex) { StrSpan span = StrUtil.Trim(chars, beginIndex, endIndex, Parser._whiteSpaces); if (span.isEmpty) { return(null); } char c = chars[span.iLeft]; if (!(c == '_' || StrUtil.IsLetter(c))) { return(null); } int 类型结束的位置的下一个字符位置 = Parser.向右寻找_不是_下划线字母数字_的字符(chars, span.iLeft + 1, span.iRight); if (类型结束的位置的下一个字符位置 == -1) { return(null); } c = chars[类型结束的位置的下一个字符位置]; if (!(StrUtil.IsOneOf(c, Parser._whiteSpaces)) || c == '*') { return(null); } int 类型后不是空白和星号的位置 = StrUtil.FindForwardUntilNot(chars, 类型结束的位置的下一个字符位置, span.iRight, Parser._whiteSpaces和星号); if (类型后不是空白和星号的位置 == -1) { return(null); } c = chars[类型后不是空白和星号的位置]; if (!(c == '_' || StrUtil.IsLetter(c))) { return(null); } int 星号Count = 0; for (int i = 类型结束的位置的下一个字符位置; i < 类型后不是空白和星号的位置; i++) { if (chars[i] == '*') { 星号Count++; } } 表达式 表达式 = 表达式_Parser.Parse(chars, 类型后不是空白和星号的位置, span.iRight); int 类型字符串长度 = 类型结束的位置的下一个字符位置 - span.iLeft; 变量声明和初始化 变量声明 = Parse_普通变量声明(chars, 表达式, span.iLeft, 类型字符串长度, 星号Count); if (变量声明 != null) { return(变量声明); } 变量声明 = Parse_数组变量声明(chars, 表达式, span.iLeft, 类型字符串长度, 星号Count); if (变量声明 != null) { return(变量声明); } return(null); }