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["tkIdentifier"] = StringResources.Get("TKIDENTIFIER");
tokenNum["tkStringLiteral"] = StringResources.Get("TKSTRINGLITERAL"); tokenNum["tkStringLiteral"] = StringResources.Get("TKSTRINGLITERAL");
tokenNum["tkAmpersend"] = "'"; tokenNum["tkAmpersend"] = "'";
tokenNum["tkColon"]="':'"; tokenNum["tkColon"] = "':'";
tokenNum["tkDotDot"]="'..'"; tokenNum["tkDotDot"] = "'..'";
tokenNum["tkPoint"]="'.'"; tokenNum["tkPoint"] = "'.'";
tokenNum["tkRoundOpen"]="'('"; tokenNum["tkRoundOpen"] = "'('";
tokenNum["tkRoundClose"]="')'"; tokenNum["tkRoundClose"] = "')'";
tokenNum["tkSemiColon"]="';'"; tokenNum["tkSemiColon"] = "';'";
tokenNum["tkSquareOpen"]="'['"; tokenNum["tkSquareOpen"] = "'['";
tokenNum["tkSquareClose"]="']'"; tokenNum["tkSquareClose"] = "']'";
tokenNum["tkQuestion"]="'?'"; tokenNum["tkQuestion"] = "'?'";
tokenNum["tkComma"]="','"; tokenNum["tkComma"] = "','";
tokenNum["tkAssign"]="':='"; tokenNum["tkAssign"] = "':='";
tokenNum["tkPlusEqual"]="'+='"; tokenNum["tkPlusEqual"] = "'+='";
tokenNum["tkMinusEqual"]="'-='"; tokenNum["tkMinusEqual"] = "'-='";
tokenNum["tkMultEqual"]="'*='"; tokenNum["tkMultEqual"] = "'*='";
tokenNum["tkDivEqual"]="'/='"; tokenNum["tkDivEqual"] = "'/='";
tokenNum["tkMinus"]="'-'"; tokenNum["tkMinus"] = "'-'";
tokenNum["tkPlus"]="'+'"; tokenNum["tkPlus"] = "'+'";
tokenNum["tkSlash"]="'//'"; tokenNum["tkSlash"] = "'//'";
tokenNum["tkStar"]="'*'"; tokenNum["tkStar"] = "'*'";
tokenNum["tkEqual"]="'='"; tokenNum["tkEqual"] = "'='";
tokenNum["tkGreater"]="'>'"; tokenNum["tkGreater"] = "'>'";
tokenNum["tkGreaterEqual"]="'>='"; tokenNum["tkGreaterEqual"] = "'>='";
tokenNum["tkLower"]="'<'"; tokenNum["tkLower"] = "'<'";
tokenNum["tkLowerEqual"]="'<='"; tokenNum["tkLowerEqual"] = "'<='";
tokenNum["tkNotEqual"]="'<>'"; tokenNum["tkNotEqual"] = "'<>'";
tokenNum["tkArrow"]="'->'"; tokenNum["tkArrow"] = "'->'";
tokenNum["tkAddressOf"]="'@'"; tokenNum["tkAddressOf"] = "'@'";
tokenNum["tkDeref"]="'^'"; tokenNum["tkDeref"] = "'^'";
tokenNum["tkStarStar"]="'**'"; tokenNum["tkStarStar"] = "'**'";
} }
public PascalParserTools(IParser parserRef) : base(parserRef) public PascalParserTools(IParser parserRef) : base(parserRef)
@ -115,6 +115,12 @@ namespace Languages.Pascal.Frontend.Core
return 0; return 0;
} }
protected override string ReplaceSpecialSymbols(string text)
{
text = text.Replace("''", "'");
return text;
}
public string CreateErrorString(string yytext, params object[] args) public string CreateErrorString(string yytext, params object[] args)
{ {
string prefix = ""; string prefix = "";
@ -128,7 +134,7 @@ namespace Languages.Pascal.Frontend.Core
yytext = "}"; yytext = "}";
prefix = StringResources.Get("FOUND{0}"); prefix = StringResources.Get("FOUND{0}");
} }
// Преобразовали в список строк - хорошо // Преобразовали в список строк - хорошо
List<string> tokens = new List<string>(args.Skip(1).Cast<string>()); 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))); // 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) 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))); literal lt;
dn.source_context = sc; if (text.Length == 3 && text[0] == '\'' && text[2] == '\'')
return dn; {
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) 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; protected const int maxCharConst = 0xFFFF;
// SSM: Errors инициализируется в другом месте - сюда только передается! // 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)); warnings.Add(new CommonWarning(res, currentFileName, loc.begin_position.line_num, loc.begin_position.column_num));
} }
protected string ReplaceSpecialSymbols(string text) protected abstract string ReplaceSpecialSymbols(string text);
{
text = text.Replace("''", "'");
return text;
}
/// <summary> /// <summary>
/// Удаление кавычек у параметра директивы /// Удаление кавычек у параметра директивы
@ -106,7 +102,7 @@ namespace PascalABCCompiler.ParserTools
{ {
if (param.Length != 1 && param.StartsWith("'") && param.EndsWith("'")) if (param.Length != 1 && param.StartsWith("'") && param.EndsWith("'"))
return param.Substring(1, param.Length - 2); return param.Substring(1, param.Length - 2);
return param; return param;
} }
@ -201,7 +197,7 @@ namespace PascalABCCompiler.ParserTools
{ {
string directiveText = ExtractDirectiveTextWithoutSpecialSymbols(directive); string directiveText = ExtractDirectiveTextWithoutSpecialSymbols(directive);
directiveName = GetDirectiveName(directiveText); directiveName = GetDirectiveName(directiveText);
ValidateDirectiveName(location, directiveName); ValidateDirectiveName(location, directiveName);
// подстрока с параметрами // подстрока с параметрами
@ -294,7 +290,7 @@ namespace PascalABCCompiler.ParserTools
{ {
string paramsNumString = ""; string paramsNumString = "";
int maxParamsNum = directiveInfo.paramsNums.Max(); int maxParamsNum = directiveInfo.paramsNums.Max();
if (directiveInfo.paramsNums.Length > 1) if (directiveInfo.paramsNums.Length > 1)
{ {
if (directiveParams.Count > maxParamsNum) if (directiveParams.Count > maxParamsNum)
@ -466,20 +462,7 @@ namespace PascalABCCompiler.ParserTools
return proc_list; return proc_list;
} }
public literal create_string_const(string text, SourceContext sc) public abstract 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 literal create_multiline_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); 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 - ужасно плодить много классов ошибок - достаточно сделать один общий для простых ошибок // SSM - 01.2014 - ужасно плодить много классов ошибок - достаточно сделать один общий для простых ошибок
public class SimpleSemanticError : CompilationErrorWithLocation public class SimpleSemanticError : CompilationErrorWithLocation
{ {
private string ErrResourceString; protected string ErrResourceString;
private object[] values; protected object[] values;
public SimpleSemanticError(location loc, string ErrResourceString, params object[] values): base(loc) 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; 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 List<TreeRealization.var_definition_node> CompiledVariables => compiledVariables;
public string UniqueNumStr() public string UniqueNumStr()