50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using System.Text.RegularExpressions;
|
|
using meowlang.parser.antlr;
|
|
|
|
namespace meowlang.parser;
|
|
|
|
public class ImportVisitorNya : MeowBaseVisitorNya<ImportModel>
|
|
{
|
|
private static readonly string PathSegmentPattern = "[a-z][a-z0-9_]*";
|
|
|
|
private static readonly Regex ProjectRegex = new Regex("^[a-z][a-z0-9_]*$");
|
|
|
|
private static readonly Regex PathRegexNoProject =
|
|
new Regex($"^({PathSegmentPattern}|\\.|\\.\\.)(/{PathSegmentPattern})*$");
|
|
|
|
private static readonly Regex PathRegex = new Regex($"^{PathSegmentPattern}(/{PathSegmentPattern})*$");
|
|
|
|
public override ImportModel VisitImportStatement(MeowParser.ImportStatementContext context)
|
|
{
|
|
var path = context.importpath.Text[1..^1].Unescape();
|
|
|
|
string? project = null;
|
|
|
|
if (path.Contains(':'))
|
|
{
|
|
var parts = path.Split(':');
|
|
project = parts[0];
|
|
if (!ProjectRegex.IsMatch(project))
|
|
{
|
|
throw new InvalidImportPathException(context.importpath, "malformed project name");
|
|
}
|
|
|
|
path = parts[1];
|
|
if (!PathRegex.IsMatch(path))
|
|
{
|
|
throw new InvalidImportPathException(context.importpath, "malformed path");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!PathRegexNoProject.IsMatch(path))
|
|
{
|
|
throw new InvalidImportPathException(context.importpath, "malformed path");
|
|
}
|
|
}
|
|
|
|
var alias = context.importname?.Text;
|
|
|
|
return new ImportModel(context.GetSpan(), project, path, alias);
|
|
}
|
|
} |