Make changes for spython compatibility (#3131)

* Update parser tools

* Update NodesBuilder

* Update syntax_tree_visitor

* Update CompilationErrors
This commit is contained in:
AlexanderZemlyak 2024-05-28 14:16:03 +03:00 committed by GitHub
parent 303a3cdb32
commit 8acf536300
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 70 additions and 66 deletions

View file

@ -30,36 +30,36 @@ namespace Languages.Pascal.Frontend.Core
tokenNum["tkIdentifier"] = StringResources.Get("TKIDENTIFIER");
tokenNum["tkStringLiteral"] = StringResources.Get("TKSTRINGLITERAL");
tokenNum["tkAmpersend"] = "'";
tokenNum["tkColon"]="':'";
tokenNum["tkDotDot"]="'..'";
tokenNum["tkPoint"]="'.'";
tokenNum["tkRoundOpen"]="'('";
tokenNum["tkRoundClose"]="')'";
tokenNum["tkSemiColon"]="';'";
tokenNum["tkSquareOpen"]="'['";
tokenNum["tkSquareClose"]="']'";
tokenNum["tkQuestion"]="'?'";
tokenNum["tkComma"]="','";
tokenNum["tkAssign"]="':='";
tokenNum["tkPlusEqual"]="'+='";
tokenNum["tkMinusEqual"]="'-='";
tokenNum["tkMultEqual"]="'*='";
tokenNum["tkDivEqual"]="'/='";
tokenNum["tkMinus"]="'-'";
tokenNum["tkPlus"]="'+'";
tokenNum["tkSlash"]="'//'";
tokenNum["tkStar"]="'*'";
tokenNum["tkEqual"]="'='";
tokenNum["tkGreater"]="'>'";
tokenNum["tkGreaterEqual"]="'>='";
tokenNum["tkLower"]="'<'";
tokenNum["tkLowerEqual"]="'<='";
tokenNum["tkNotEqual"]="'<>'";
tokenNum["tkArrow"]="'->'";
tokenNum["tkAddressOf"]="'@'";
tokenNum["tkDeref"]="'^'";
tokenNum["tkStarStar"]="'**'";
tokenNum["tkColon"] = "':'";
tokenNum["tkDotDot"] = "'..'";
tokenNum["tkPoint"] = "'.'";
tokenNum["tkRoundOpen"] = "'('";
tokenNum["tkRoundClose"] = "')'";
tokenNum["tkSemiColon"] = "';'";
tokenNum["tkSquareOpen"] = "'['";
tokenNum["tkSquareClose"] = "']'";
tokenNum["tkQuestion"] = "'?'";
tokenNum["tkComma"] = "','";
tokenNum["tkAssign"] = "':='";
tokenNum["tkPlusEqual"] = "'+='";
tokenNum["tkMinusEqual"] = "'-='";
tokenNum["tkMultEqual"] = "'*='";
tokenNum["tkDivEqual"] = "'/='";
tokenNum["tkMinus"] = "'-'";
tokenNum["tkPlus"] = "'+'";
tokenNum["tkSlash"] = "'//'";
tokenNum["tkStar"] = "'*'";
tokenNum["tkEqual"] = "'='";
tokenNum["tkGreater"] = "'>'";
tokenNum["tkGreaterEqual"] = "'>='";
tokenNum["tkLower"] = "'<'";
tokenNum["tkLowerEqual"] = "'<='";
tokenNum["tkNotEqual"] = "'<>'";
tokenNum["tkArrow"] = "'->'";
tokenNum["tkAddressOf"] = "'@'";
tokenNum["tkDeref"] = "'^'";
tokenNum["tkStarStar"] = "'**'";
}
public PascalParserTools(IParser parserRef) : base(parserRef)
@ -115,6 +115,12 @@ namespace Languages.Pascal.Frontend.Core
return 0;
}
protected override string ReplaceSpecialSymbols(string text)
{
text = text.Replace("''", "'");
return text;
}
public string CreateErrorString(string yytext, params object[] args)
{
string prefix = "";
@ -128,7 +134,7 @@ namespace Languages.Pascal.Frontend.Core
yytext = "}";
prefix = StringResources.Get("FOUND{0}");
}
// Преобразовали в список строк - хорошо
List<string> tokens = new List<string>(args.Skip(1).Cast<string>());
@ -178,7 +184,7 @@ namespace Languages.Pascal.Frontend.Core
// string w = string.Join(" или ", tokens.Select(s => ConvertToHumanName((string)s)));
return string.Format(prefix + ExpectedString, "'" + yytext + "'", MaxTokHuman);
return string.Format(prefix + ExpectedString, "'" + yytext + "'", MaxTokHuman);
}
public string directive_parameter(string s)
@ -194,11 +200,19 @@ namespace Languages.Pascal.Frontend.Core
}
}
public ident create_directive_name(string text, SourceContext sc)
public override literal create_string_const(string text, SourceContext sc)
{
ident dn = new ident(new string(text.ToCharArray(1, text.Length - 1)));
dn.source_context = sc;
return dn;
literal lt;
if (text.Length == 3 && text[0] == '\'' && text[2] == '\'')
{
lt = new char_const(text[1]);
lt.source_context = sc;
return lt;
}
text = ReplaceSpecialSymbols(text.Substring(1, text.Length - 2));
lt = new string_const(text);
lt.source_context = sc;
return lt;
}
public function_lambda_definition find_pascalABC_lambda_name(string name)

View file

@ -27,7 +27,7 @@ namespace PascalABCCompiler.ParserTools
// Класс глобальных описаний и статических методов
// для использования различными подсистемами парсера и сканера
public abstract class BaseParserTools
public abstract class BaseParserTools
{
protected const int maxCharConst = 0xFFFF;
// SSM: Errors инициализируется в другом месте - сюда только передается!
@ -93,11 +93,7 @@ namespace PascalABCCompiler.ParserTools
warnings.Add(new CommonWarning(res, currentFileName, loc.begin_position.line_num, loc.begin_position.column_num));
}
protected string ReplaceSpecialSymbols(string text)
{
text = text.Replace("''", "'");
return text;
}
protected abstract string ReplaceSpecialSymbols(string text);
/// <summary>
/// Удаление кавычек у параметра директивы
@ -106,7 +102,7 @@ namespace PascalABCCompiler.ParserTools
{
if (param.Length != 1 && param.StartsWith("'") && param.EndsWith("'"))
return param.Substring(1, param.Length - 2);
return param;
}
@ -201,7 +197,7 @@ namespace PascalABCCompiler.ParserTools
{
string directiveText = ExtractDirectiveTextWithoutSpecialSymbols(directive);
directiveName = GetDirectiveName(directiveText);
ValidateDirectiveName(location, directiveName);
// подстрока с параметрами
@ -294,7 +290,7 @@ namespace PascalABCCompiler.ParserTools
{
string paramsNumString = "";
int maxParamsNum = directiveInfo.paramsNums.Max();
if (directiveInfo.paramsNums.Length > 1)
{
if (directiveParams.Count > maxParamsNum)
@ -466,20 +462,7 @@ namespace PascalABCCompiler.ParserTools
return proc_list;
}
public literal create_string_const(string text, SourceContext sc)
{
literal lt;
if (text.Length == 3 && text[0] == '\'' && text[2] == '\'')
{
lt = new char_const(text[1]);
lt.source_context = sc;
return lt;
}
text = ReplaceSpecialSymbols(text.Substring(1, text.Length - 2));
lt = new string_const(text);
lt.source_context = sc;
return lt;
}
public abstract literal create_string_const(string text, SourceContext sc);
public literal create_multiline_string_const(string text, SourceContext sc)
{

View file

@ -691,5 +691,17 @@ namespace PascalABCCompiler.SyntaxTree
return BuildShortProcFuncDefinitionNoSC(ff, ass);
}
public static var_statement BuildVarStatementNodeFromAssignNode(assign _assign)
{
ident to = _assign.to as ident;
if (to == null)
throw new NotImplementedException();
var _var_statement = SyntaxTreeNodesConstructor.CreateVarStatementNode(new ident_list(to.name), null, _assign.from);
_var_statement.source_context = _assign.source_context;
_var_statement.var_def.source_context = _assign.source_context;
_var_statement.var_def.vars.source_context = _assign.to.source_context;
return _var_statement;
}
}
}

View file

@ -133,8 +133,8 @@ namespace PascalABCCompiler.TreeConverter
// SSM - 01.2014 - ужасно плодить много классов ошибок - достаточно сделать один общий для простых ошибок
public class SimpleSemanticError : CompilationErrorWithLocation
{
private string ErrResourceString;
private object[] values;
protected string ErrResourceString;
protected object[] values;
public SimpleSemanticError(location loc, string ErrResourceString, params object[] values): base(loc)
{

View file

@ -158,11 +158,6 @@ namespace PascalABCCompiler.TreeConverter
SystemLibrary.SystemLibrary.syn_visitor = this;
}
public void ProcessNode<T>(T cd) where T : syntax_tree_node
{
base.ProcessNode(cd);
}
public List<TreeRealization.var_definition_node> CompiledVariables => compiledVariables;
public string UniqueNumStr()