mirror of
https://github.com/ZuneDev/ZuneUIXTools.git
synced 2026-07-27 13:11:59 -07:00
Add line and column numbers to lexer tree
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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}:";
|
||||
|
||||
Reference in New Issue
Block a user