Add line and column numbers to lexer tree

This commit is contained in:
Yoshi Askharoun
2024-02-04 22:09:21 -06:00
parent 6823ef5296
commit 934c20ccdf
5 changed files with 30 additions and 10 deletions
+12 -5
View File
@@ -7,11 +7,11 @@ namespace Microsoft.Iris.Asm;
partial class Lexer
{
public static readonly Parser<IBodyItem> BodyItem = ParseBodyItem;
private static IResult<IBodyItem> ParseBodyItem(IInput input)
{
input = ConsumeWhitespace(input);
var line = input.Line;
var column = input.Column;
var identifierResult = Parse.Letter.AtLeastOnce().Text().Invoke(input);
if (!identifierResult.WasSuccessful)
@@ -24,7 +24,10 @@ partial class Lexer
if (!input.AtEnd && input.Current == ':')
{
input = input.Advance();
bodyItem = new Label(identifier);
bodyItem = new Label(identifier)
{
Line = line, Column = column,
};
input = StatementEnd(input).Remainder;
}
else
@@ -38,11 +41,15 @@ partial class Lexer
input = ConsumeWhitespace(input);
var operandsResult = Parse.Ref(() => AlphanumericText).DelimitedBy(Parse.Char(',').Token())(input);
operands.AddRange(operandsResult.Value.Select(s => new Operand(s)));
operands.AddRange(operandsResult.Value.Select(s => new Operand(s) { Line = line }));
input = operandsResult.Remainder;
}
bodyItem = new Instruction(identifier, operands);
bodyItem = new Instruction(identifier, operands)
{
Line = line,
Column = column,
};
}
return Result.Success(bodyItem, input);
-2
View File
@@ -5,8 +5,6 @@ namespace Microsoft.Iris.Asm;
partial class Lexer
{
public static readonly Parser<IImport> Import = ParseImport;
private static IResult<IImport> ParseImport(IInput input)
{
input = ConsumeWhitespace(input);
+4
View File
@@ -17,6 +17,10 @@ public static partial class Lexer
from sectionId in Parse.Letter.AtLeastOnce().Text()
select sectionId;
public static readonly Parser<IImport> Import = ParseImport;
public static readonly Parser<IBodyItem> BodyItem = ParseBodyItem;
public static readonly Parser<Program> Program =
from imports in Import.Token().Many()
from body in BodyItem.Many()
+2
View File
@@ -10,6 +10,8 @@ public abstract record AsmItem : IAsmItem
{
public int Line { get; set; } = -1;
public int Column { get; set; } = -1;
internal const string DebuggerDisplay = "({Line}, {Column})";
}
public interface IDirective : IAsmItem;
+12 -3
View File
@@ -1,10 +1,12 @@
using Microsoft.Iris.Markup;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Microsoft.Iris.Asm.Models;
[DebuggerDisplay("{ToString()} " + DebuggerDisplay)]
public record Instruction(string Mnemonic, IEnumerable<Operand> Operands) : BodyItem
{
public Instruction(OpCode opCode, OperationType? operationType, IEnumerable<Operand> Operands)
@@ -19,11 +21,18 @@ public record Instruction(string Mnemonic, IEnumerable<Operand> Operands) : Body
public OpCode OpCode => LexerMaps.MnemonicToOpCode(Mnemonic);
public OperationType? OperationType => LexerMaps.TryOperationMnemonicToType(Mnemonic);
public override string ToString() => Operands.Any()
? $"{Mnemonic} {string.Join(", ", Operands)}"
: Mnemonic;
public override string ToString() => ToString(true);
public string ToString(bool uppercase)
{
var mnemonic = uppercase ? Mnemonic.ToUpperInvariant() : Mnemonic.ToLowerInvariant();
return Operands.Any()
? $"{mnemonic} {string.Join(", ", Operands)}"
: mnemonic;
}
}
[DebuggerDisplay("{Name} " + DebuggerDisplay)]
public record Label(string Name) : BodyItem
{
public override string ToString() => $"{Name}:";