/** * Executed after all other tokens are added. Literal tokens are * segmented from the original source code, separated by the ranges * of the other tokens. */ internal static void AddLiteralTokens(string literal, List <MetaToken> tokens) { for (int i = 0, size = tokens.Count; i <= size; ++i) { int startIndex; int length = 0; if (i == 0) { startIndex = 0; } else { startIndex = tokens[i - 1].startIndex + tokens[i - 1].length; } if (i < size) { length = tokens[i].startIndex - startIndex; } else { length = literal.Length - startIndex; } if (length > 0) { tokens.Add(MetaToken.LITERAL(literal, startIndex, length)); } } tokens.SortAndReduce(); }
private string META_GetListSize(MetaToken listvar) { var list_prefix = LISTVAR_GetListName(listvar.text[0]); var list_var = META_GetList(list_prefix); return("(" + list_var + ")->size()"); }
internal static void SortAndReduce(this List <MetaToken> tokens) { tokens.Sort((a, b) => { var ret = a.startIndex - b.startIndex; if (ret == 0) { ret = a.length - b.length; if (ret == 0) { if (a.type != b.type) { Console.WriteLine(a.ToString()); Console.WriteLine(b.ToString()); MetaToken.Throw("Different tokens with same range"); } } } return(ret); }); for (int i = 1; i < tokens.Count; ++i) { if (tokens[i - 1].startIndex + tokens[i - 1].length >= tokens[i].startIndex + tokens[i].length) { tokens.RemoveAt(i); --i; } } }
public static MetaToken GeneralFactory(string text, int startIdx, int len, string type_str, int param_count) { var meta = new MetaToken(); var type = (MetaType)Enum.Parse(typeof(MetaType), type_str); meta.original_text = text; try { meta.startIndex = startIdx; meta.length = len; meta.type = type; meta.text = CaptureArguments(text, startIdx, len); CheckParamCount(meta.text, param_count, type_str); } catch (Exception e) { Console.WriteLine("Current token:"); Console.WriteLine(meta); Console.WriteLine("Text:"); Console.WriteLine(text.Substring(startIdx, len)); throw; } return(meta); }
private void META_SetListDepth(MetaToken listvar) { //XXX currently nested iteration over a same list is impossible, //as we only have a flat dict string->int for the iteration depth, //and inner depth will overwrite the outer one. var list_prefix = LISTVAR_GetListName(listvar.text[0]); d_list_iterator_depth[list_prefix] = state.foreach_depth; }
public static MetaToken END(string text, int startIdx, int len) { var meta = new MetaToken(); meta.startIndex = startIdx; meta.length = len; meta.type = MetaType.END; meta.original_text = text; return(meta); }
public static MetaToken TEMPLATE(string text, int startIdx, int len) { var meta = new MetaToken(); meta.startIndex = startIdx; meta.length = len; meta.type = MetaType.TEMPLATE; meta.original_text = text; meta.text.Add(text.Substring(startIdx, len)); return(meta); }
private bool Is_LISTVAR(MetaToken i) { if (i.type == MetaType.USE_LIST) { return(true); } if (i.type != MetaType.TEMPLATE) { return(false); } var template_name = i.text[0]; if (null != LISTVAR_GetListName(template_name)) { return(true); } return(false); }
internal static void InsertFromRegex(this List <MetaToken> tokens, string literal, Expression <Func <Regex> > rgx_func) { var rgx = rgx_func.Compile()(); var rgx_name = rgx_func.ToString(); int idx = rgx_name.LastIndexOf('.') + 1; int len = rgx_name.Length - idx; rgx_name = rgx_name.Substring(idx, len); foreach (var attr in typeof(MetaType).GetMember(rgx_name)[0].GetCustomAttributes(false)) { var param_attr = (PARAMAttribute)attr; if (param_attr != null) { tokens.AddRange(from Match m in rgx.Matches(literal) select MetaToken.GeneralFactory(literal, m.Index, m.Length, rgx_name, param_attr.param_count)); return; } } var meta_gen = typeof(MetaToken).GetMethod(rgx_name).CreateDelegate(typeof(META_TOKEN_GEN_FUNC)) as META_TOKEN_GEN_FUNC; tokens.AddRange(from Match m in rgx.Matches(literal) select meta_gen(literal, m.Index, m.Length)); }