private void CollectHighlight(ABnfGuess target_guess, ABnfElement element, List <ALanguageHighlightWordInfo> list) { if (element is ALittleScriptPropertyValueCustomTypeElement || element is ALittleScriptVarAssignNameDecElement) { if (element.GetElementText() != m_key) { return; } var error = element.GuessType(out ABnfGuess guess); if (error != null) { return; } if (guess.GetValue() == target_guess.GetValue()) { var info = new ALanguageHighlightWordInfo(); info.start = element.GetStart(); info.end = element.GetEnd(); list.Add(info); } return; } if (element is ABnfNodeElement) { var childs = (element as ABnfNodeElement).GetChilds(); foreach (var child in childs) { CollectHighlight(target_guess, child, list); } } }
public override void UpdateValue() { value = ""; if (is_const) { value += "const "; } value += "Map<" + key_type.GetValue() + "," + value_type.GetValue() + ">"; }
public override void UpdateValue() { value = ""; if (is_const) { value += "const "; } if (is_native) { value += "native "; } value += "List<" + sub_type.GetValue() + ">"; }
public override string GetTotalValue() { var v = ""; if (is_const) { v += "const "; } v += native_value; if (template_extends != null) { return(v + ":" + template_extends.GetValue()); } else if (is_class) { return(v + ":class"); } else if (is_struct) { return(v + ":struct"); } return(v); }
private ABnfGuessError AnalysisTemplate(Dictionary <string, ABnfGuess> fill_map, ABnfGuess left_guess, ABnfElement right_src, ABnfGuess right_guess, bool assign_or_call) { // 如果值等于null,那么可以赋值 if (right_guess.GetValue() == "null") { return(null); } // const是否可以赋值给非const if (assign_or_call) { if (left_guess.is_const && !right_guess.is_const) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ", 不能是:" + right_guess.GetValue())); } } else { // 如果不是基本变量类型(排除any),基本都是值传递,函数调用时就不用检查const if (!(left_guess is ALittleScriptGuessPrimitive) || left_guess.GetValueWithoutConst() == "any") { if (!left_guess.is_const && right_guess.is_const) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ", 不能是:" + right_guess.GetValue())); } } } // 如果任何一方是any,那么就认为可以相等 if (left_guess is ALittleScriptGuessAny) { return(null); } if (left_guess is ALittleScriptGuessPrimitive || left_guess is ALittleScriptGuessStruct) { return(ALittleScriptOp.GuessTypeEqual(left_guess, right_src, right_guess, assign_or_call, false)); } if (left_guess is ALittleScriptGuessMap) { if (!(right_guess is ALittleScriptGuessMap)) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } var error = AnalysisTemplate(fill_map, ((ALittleScriptGuessMap)left_guess).key_type, right_src, ((ALittleScriptGuessMap)right_guess).key_type, false); if (error != null) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } error = AnalysisTemplate(fill_map, ((ALittleScriptGuessMap)left_guess).value_type, right_src, ((ALittleScriptGuessMap)right_guess).value_type, false); if (error != null) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } return(null); } if (left_guess is ALittleScriptGuessList) { if (!(right_guess is ALittleScriptGuessList)) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } var error = AnalysisTemplate(fill_map, ((ALittleScriptGuessList)left_guess).sub_type, right_src, ((ALittleScriptGuessList)right_guess).sub_type, false); if (error != null) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } return(null); } if (left_guess is ALittleScriptGuessFunctor) { if (!(right_guess is ALittleScriptGuessFunctor)) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } ALittleScriptGuessFunctor left_guess_functor = (ALittleScriptGuessFunctor)left_guess; ALittleScriptGuessFunctor right_guess_functor = (ALittleScriptGuessFunctor)right_guess; if (left_guess_functor.param_list.Count != right_guess_functor.param_list.Count || left_guess_functor.param_nullable_list.Count != right_guess_functor.param_nullable_list.Count || left_guess_functor.return_list.Count != right_guess_functor.return_list.Count || left_guess_functor.template_param_list.Count != right_guess_functor.template_param_list.Count || left_guess_functor.await_modifier != right_guess_functor.await_modifier || left_guess_functor.proto == null && right_guess_functor.proto != null || left_guess_functor.proto != null && right_guess_functor.proto == null || (left_guess_functor.proto != null && left_guess_functor.proto != right_guess_functor.proto) || left_guess_functor.param_tail == null && right_guess_functor.param_tail != null || left_guess_functor.param_tail != null && right_guess_functor.param_tail == null || left_guess_functor.return_tail == null && right_guess_functor.return_tail != null || left_guess_functor.return_tail != null && right_guess_functor.return_tail == null ) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } for (int i = 0; i < left_guess_functor.template_param_list.Count; ++i) { var error = AnalysisTemplate(fill_map, left_guess_functor.template_param_list[i], right_src, right_guess_functor.template_param_list[i], false); if (error != null) { return(error); } } for (int i = 0; i < left_guess_functor.param_list.Count; ++i) { var error = AnalysisTemplate(fill_map, left_guess_functor.param_list[i], right_src, right_guess_functor.param_list[i], false); if (error != null) { return(error); } } for (int i = 0; i < left_guess_functor.param_nullable_list.Count; ++i) { if (left_guess_functor.param_nullable_list[i] != right_guess_functor.param_nullable_list[i]) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } } for (int i = 0; i < left_guess_functor.return_list.Count; ++i) { var error = AnalysisTemplate(fill_map, left_guess_functor.return_list[i], right_src, right_guess_functor.return_list[i], false); if (error != null) { return(error); } } return(null); } if (left_guess is ALittleScriptGuessClass) { if (right_guess is ALittleScriptGuessTemplate) { right_guess = (right_guess as ALittleScriptGuessTemplate).template_extends; } if (!(right_guess is ALittleScriptGuessClass)) { return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } if (left_guess.GetValue() == right_guess.GetValue()) { return(null); } var error = ALittleScriptUtility.IsClassSuper((left_guess as ALittleScriptGuessClass).class_dec, right_guess.GetValue(), out bool result); if (error != null) { return(error); } if (result) { return(null); } error = ALittleScriptUtility.IsClassSuper((right_guess as ALittleScriptGuessClass).class_dec, left_guess.GetValue(), out result); if (error != null) { return(error); } if (result) { return(null); } return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } if (left_guess is ALittleScriptGuessTemplate) { var left_guess_template = left_guess as ALittleScriptGuessTemplate; // 查看模板是否已经被填充,那么就按填充的检查 if (fill_map.TryGetValue(left_guess_template.GetValue(), out ABnfGuess fill_guess)) { return(ALittleScriptOp.GuessTypeEqual(fill_guess, right_src, right_guess, false, false)); } // 处理还未填充 if (left_guess_template.template_extends != null) { var error = AnalysisTemplate(fill_map, left_guess_template.template_extends, right_src, right_guess, false); if (error != null) { return(error); } if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess); return(null); } else if (left_guess_template.is_class) { if (right_guess is ALittleScriptGuessClass) { if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess); return(null); } else if (right_guess is ALittleScriptGuessTemplate) { var right_guess_template = right_guess as ALittleScriptGuessTemplate; if (right_guess_template.template_extends is ALittleScriptGuessClass || right_guess_template.is_class) { if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(right_guess_template.GetValueWithoutConst(), right_guess); return(null); } } return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } else if (left_guess_template.is_struct) { if (right_guess is ALittleScriptGuessStruct) { if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess); return(null); } else if (right_guess is ALittleScriptGuessTemplate) { var right_guess_template = right_guess as ALittleScriptGuessTemplate; if (right_guess_template.template_extends is ALittleScriptGuessStruct || right_guess_template.is_struct) { if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValue(), right_guess); return(null); } } return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); } if (right_guess.is_const) { right_guess = right_guess.Clone(); right_guess.is_const = false; right_guess.UpdateValue(); } fill_map.Add(left_guess_template.GetValueWithoutConst(), right_guess); return(null); } return(new ABnfGuessError(right_src, "要求是" + left_guess.GetValue() + ",不能是:" + right_guess.GetValue())); }
public override void UpdateValue() { value = "Functor<"; // proto和await修饰 List <string> pre_list = new List <string>(); if (proto != null) { pre_list.Add(proto); } if (const_modifier) { pre_list.Add("const"); } if (await_modifier) { pre_list.Add("await"); } value += string.Join(",", pre_list); // 模板参数列表 if (template_param_list.Count > 0) { List <string> template_string_list = new List <string>(); foreach (var guess in template_param_list) { template_string_list.Add(guess.GetTotalValue()); } value += "<" + string.Join(",", template_string_list) + ">"; } // 参数类型列表 List <string> param_string_list = new List <string>(); for (int i = 0; i < param_list.Count; ++i) { if (i < param_nullable_list.Count && param_nullable_list[i]) { param_string_list.Add("[Nullable] " + param_list[i].GetValue()); } else { param_string_list.Add(param_list[i].GetValue()); } } if (param_tail != null) { param_string_list.Add(param_tail.GetValue()); } value += "(" + string.Join(",", param_string_list) + ")"; // 返回值类型列表 List <string> return_string_list = new List <string>(); foreach (var guess in return_list) { return_string_list.Add(guess.GetValue()); } if (return_tail != null) { return_string_list.Add(return_tail.GetValue()); } if (return_string_list.Count > 0) { value += ":"; } value += string.Join(",", return_string_list); value += ">"; }
public ALittleScriptIndex() { // 基础变量 sPrimitiveGuessMap = new Dictionary <string, ABnfGuess>(); sPrimitiveGuessListMap = new Dictionary <string, List <ABnfGuess> >(); List <ABnfGuess> tmp; tmp = new List <ABnfGuess>(); sIntGuess = new ALittleScriptGuessInt(false); tmp.Add(sIntGuess); sPrimitiveGuessListMap.Add(sIntGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sIntGuess.GetValue(), sIntGuess); tmp = new List <ABnfGuess>(); sDoubleGuess = new ALittleScriptGuessDouble(false); tmp.Add(sDoubleGuess); sPrimitiveGuessListMap.Add(sDoubleGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sDoubleGuess.GetValue(), sDoubleGuess); tmp = new List <ABnfGuess>(); sStringGuess = new ALittleScriptGuessString(false); tmp.Add(sStringGuess); sPrimitiveGuessListMap.Add(sStringGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sStringGuess.GetValue(), sStringGuess); tmp = new List <ABnfGuess>(); sBoolGuess = new ALittleScriptGuessBool(false); tmp.Add(sBoolGuess); sPrimitiveGuessListMap.Add(sBoolGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sBoolGuess.GetValue(), sBoolGuess); tmp = new List <ABnfGuess>(); sLongGuess = new ALittleScriptGuessLong(false); tmp.Add(sLongGuess); sPrimitiveGuessListMap.Add(sLongGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sLongGuess.GetValue(), sLongGuess); tmp = new List <ABnfGuess>(); sAnyGuess = new ALittleScriptGuessAny(false); tmp.Add(sAnyGuess); sPrimitiveGuessListMap.Add(sAnyGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sAnyGuess.GetValue(), sAnyGuess); tmp = new List <ABnfGuess>(); sConstIntGuess = new ALittleScriptGuessInt(true); tmp.Add(sConstIntGuess); sPrimitiveGuessListMap.Add(sConstIntGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sConstIntGuess.GetValue(), sConstIntGuess); tmp = new List <ABnfGuess>(); sConstDoubleGuess = new ALittleScriptGuessDouble(true); tmp.Add(sConstDoubleGuess); sPrimitiveGuessListMap.Add(sConstDoubleGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sConstDoubleGuess.GetValue(), sConstDoubleGuess); tmp = new List <ABnfGuess>(); sConstStringGuess = new ALittleScriptGuessString(true); tmp.Add(sConstStringGuess); sPrimitiveGuessListMap.Add(sConstStringGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sConstStringGuess.GetValue(), sConstStringGuess); tmp = new List <ABnfGuess>(); sConstBoolGuess = new ALittleScriptGuessBool(true); tmp.Add(sConstBoolGuess); sPrimitiveGuessListMap.Add(sConstBoolGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sConstBoolGuess.GetValue(), sConstBoolGuess); tmp = new List <ABnfGuess>(); sConstLongGuess = new ALittleScriptGuessLong(true); tmp.Add(sConstLongGuess); sPrimitiveGuessListMap.Add(sConstLongGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sConstLongGuess.GetValue(), sConstLongGuess); tmp = new List <ABnfGuess>(); sConstAnyGuess = new ALittleScriptGuessAny(true); tmp.Add(sConstAnyGuess); sPrimitiveGuessListMap.Add(sConstAnyGuess.GetValue(), tmp); sPrimitiveGuessMap.Add(sConstAnyGuess.GetValue(), sConstAnyGuess); // null常量 sConstNullGuess = new List <ABnfGuess>(); sConstNullGuess.Add(new ALittleScriptGuessConst("null")); // 控制关键字 sCtrlKeyWord.Add("if"); sCtrlKeyWord.Add("elseif"); sCtrlKeyWord.Add("else"); sCtrlKeyWord.Add("do"); sCtrlKeyWord.Add("while"); sCtrlKeyWord.Add("return"); sCtrlKeyWord.Add("break"); sCtrlKeyWord.Add("continue"); sCtrlKeyWord.Add("for"); sCtrlKeyWord.Add("in"); sCtrlKeyWord.Add("throw"); sCtrlKeyWord.Add("assert"); }
public override ABnfGuessError GuessTypes(out List <ABnfGuess> guess_list) { guess_list = new List <ABnfGuess>(); m_getter_list = null; m_setter_list = null; m_class_guess = null; var error = CalcResolve(out List <ABnfElement> result_list, out ABnfGuess pre_type); if (error != null) { return(error); } foreach (var result in result_list) { ABnfGuess guess = null; if (result is ALittleScriptClassVarDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } if (m_class_guess != null && guess is ALittleScriptGuessTemplate) { if (!m_class_guess.template_map.TryGetValue(guess.GetValueWithoutConst(), out ABnfGuess guess_template)) { for (int i = 0; i < m_class_guess.template_list.Count; ++i) { guess_template = m_class_guess.template_list[i]; if (guess_template.GetValueWithoutConst() == guess.GetValueWithoutConst()) { break; } } } if (guess_template != null) { if (guess.is_const && !guess_template.is_const) { guess_template = guess_template.Clone(); guess_template.is_const = true; guess_template.UpdateValue(); } guess = guess_template; } } } else if (result is ALittleScriptStructVarDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } } else if (result is ALittleScriptEnumVarDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } } else if (result is ALittleScriptMethodNameDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } // 如果前一个数据是const,那么调用的函数也必须是const if (pre_type != null && pre_type.is_const) { var guess_functor = guess as ALittleScriptGuessFunctor; if (guess_functor != null && !guess_functor.const_modifier) { return(new ABnfGuessError(m_element, "请使用带Const修饰的函数")); } } if (result.GetParent() is ALittleScriptClassGetterDecElement) { if (m_getter_list != null && m_getter_list.IndexOf(result) >= 0 && guess is ALittleScriptGuessFunctor) { guess = ((ALittleScriptGuessFunctor)guess).return_list[0]; } } else if (result.GetParent() is ALittleScriptClassSetterDecElement) { if (m_setter_list != null && m_setter_list.IndexOf(result) >= 0 && guess is ALittleScriptGuessFunctor) { guess = ((ALittleScriptGuessFunctor)guess).param_list[1]; } } error = ReplaceTemplate(guess, out guess); if (error != null) { return(error); } } else if (result is ALittleScriptVarAssignNameDecElement) { error = result.GuessType(out guess); if (error != null) { return(error); } } else if (result is ALittleScriptEnumNameDecElement) { error = result.GuessType(out ABnfGuess enum_guess); if (error != null) { return(error); } if (!(enum_guess is ALittleScriptGuessEnum)) { return(new ABnfGuessError(m_element, "ALittleEnumNameDec.guessType的结果不是ALittleGuessEnum")); } var enum_guess_enum = enum_guess as ALittleScriptGuessEnum; var info = new ALittleScriptGuessEnumName(enum_guess_enum.namespace_name, enum_guess_enum.enum_name, result as ALittleScriptEnumNameDecElement); info.UpdateValue(); guess = info; } else if (result is ALittleScriptStructNameDecElement) { error = result.GuessType(out ABnfGuess struct_guess); if (error != null) { return(error); } if (!(struct_guess is ALittleScriptGuessStruct)) { return(new ABnfGuessError(m_element, "ALittleStructNameDec.guessType的结果不是ALittleGuessStruct")); } var struct_guess_struct = struct_guess as ALittleScriptGuessStruct; var info = new ALittleScriptGuessStructName(struct_guess_struct.namespace_name, struct_guess_struct.struct_name, result as ALittleScriptStructNameDecElement); info.UpdateValue(); guess = info; } else if (result is ALittleScriptClassNameDecElement) { error = result.GuessType(out ABnfGuess class_guess); if (error != null) { return(error); } if (!(class_guess is ALittleScriptGuessClass)) { return(new ABnfGuessError(m_element, "ALittleClassNameDec.guessType的结果不是ALittleGuessClass")); } var class_guess_class = class_guess as ALittleScriptGuessClass; if (class_guess_class.template_list.Count > 0) { return(new ABnfGuessError(m_element, "模板类" + class_guess_class.GetValue() + "不能直接使用")); } var info = new ALittleScriptGuessClassName(class_guess_class.namespace_name, class_guess_class.class_name, result as ALittleScriptClassNameDecElement); info.UpdateValue(); guess = info; } if (guess != null) { if (pre_type != null && pre_type.is_const && !guess.is_const) { if (guess is ALittleScriptGuessPrimitive) { var guess_value = guess.GetValue(); ALittleScriptIndex.inst.sPrimitiveGuessMap.TryGetValue("const " + guess.GetValue(), out guess); if (guess == null) { return(new ABnfGuessError(m_element, "找不到const " + guess_value)); } } else { guess = guess.Clone(); guess.is_const = true; guess.UpdateValue(); } } guess_list.Add(guess); } } m_getter_list = null; m_setter_list = null; m_class_guess = null; return(null); }