feenkcom / gtoolkit

Glamorous Toolkit is the Moldable Development environment. It empowers you to make systems explainable through experiences tailored for each problem.
https://gtoolkit.com
MIT License
1.06k stars 46 forks source link

Parsing modern C# code results in a "Token not expected =>' error #3822

Closed macta closed 1 week ago

macta commented 3 weeks ago

When parsing C# files in our project, most files parse correctly but I have encountered some that give a "token not expected" error (on a project that compiles) - which I believe is due to changes in the C# language definition.

Looking at the parser view in the error it is highlighting the => expression in the code:

image
macta commented 3 weeks ago

The actual source in question is the following (namespace names have been changed - but that shouldnt impact the parsing which I can create with this sampel:

source := '
using FluentNHibernate.Mapping;
using MyProject.DataClasses.Locations;
using MyProject.Nhibernate.UserTypes;

namespace MyProject.Nhibernate.Mappings
{
    public class LocationMapping : ClassMap<Location>
    {
        public LocationMapping()
        {
            Table("Locations");
            BatchSize(50);
            Cache.ReadWrite();

            Id(x => x.Id).GeneratedBy.Assigned();

            Map(x => x.LocationName).Column("Name");
#pragma warning disable 618
            Map(x => x.InternalPrintName).Column("[PrintName]");
#pragma warning restore 618
            Map(x => x.TimeZone).Column("TimeZoneId").CustomType(typeof(TimeZoneUserType));
            Map(x => x.Coordinates).Column("Coordinates").CustomType(typeof(CoordsUserType));

            Map(x => x.LocationCode);

            References(x => x.Country)
                .Column("Countrycode")
                .Fetch.Join()
                .Not.LazyLoad()
                .Cascade.None();

            References(x => x.SubDivision)
                .Column("SubDivisionId")
                .Fetch.Join()
                .Not.LazyLoad()
                .Cascade.None();

            DiscriminateSubClassesOnColumn("").Formula(GetDiscriminatorFormula());
        }

        private static string GetDiscriminatorFormula() =>
            "CASE " + $"WHEN NOT OfficeCode IS NULL THEN ''{nameof(OLocation)}'' "
                    + $"WHEN NOT IATCode IS NULL THEN ''{nameof(ALocation)}'' "
                    + $"WHEN NOT AgentOfCode IS NULL THEN ''{nameof(AgtLocation)}''"
                    + $"WHEN NOT DropOffLocationCode IS NULL THEN ''{nameof(DropOffLocation)}''"
                    + $"ELSE ''{nameof(PlaceLocation)}'' " + "END";
    }

    public class OfficeLocationMapping : SubclassMap<OfficeLocation>
    {
        public OfficeLocationMapping() => Map(x => x.OfficeCode);
    }

    public class AirportLocationMapping : SubclassMap<AirportLocation>
    {
        public AirportLocationMapping() => Map(x => x.IATACode);
    }

    public class AgentLocationMapping : SubclassMap<AgentLocation>
    {
        public AgentLocationMapping() =>
            Map(x => x.AgentOfficeCode);
    }

    public class DropOffLocationMapping : SubclassMap<DropOffLocation>
    {
        public DropOffLocationMapping() => Map(x => x.DropOffLocationCode);
    }

    public class PlaceLocationMapping : SubclassMap<PlaceLocation>
    {
    }
}
'.

CSharpParser parse: source
macta commented 1 week ago

How can you easily see the changes that were made to the parser/grammar to get a feel for what these kinds of errors imply? #3822 is just the baseline version changes - but am interested in what the specific issue changes were?

I ask as these changes got me running with the parsing fluent nHibernate mapping files - but other source code using later C# features will need further modfications. @girba should I add them to this issue - or start a new issue (understanding that it might take a while to fix)

girba commented 1 week ago

please add a new issue