Skip to content

Commit

Permalink
Add SyntaxKind, fix Visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Feb 4, 2019
1 parent 6d14fef commit 4a766e4
Show file tree
Hide file tree
Showing 35 changed files with 503 additions and 179 deletions.
2 changes: 1 addition & 1 deletion src/SharpToml.Tests/StandardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void SpecInvalid(string inputName, string toml, string json)

private static void ValidateSpec(string type, string inputName, string toml, string json)
{
var doc = Toml.Parse(toml);
var doc = Toml.ParseAndValidate(toml);

Console.WriteLine();
DisplayHeader("input");
Expand Down
11 changes: 5 additions & 6 deletions src/SharpToml/Parsing/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ private void ReadNumberOrDate(char32? signPrefix = null, TextPosition? signPrefi
}

// Parse leading digits
ReadDigits(ref end);
ReadDigits(ref end, hasLeadingZero);

// We are in the case of a date
if (_c == '-' || _c == ':')
Expand Down Expand Up @@ -583,7 +583,7 @@ private void ReadNumberOrDate(char32? signPrefix = null, TextPosition? signPrefi
}

isFloat = true;
ReadDigits(ref end);
ReadDigits(ref end, false);
}

// Parse only the exponent if we don't have a range
Expand All @@ -607,7 +607,7 @@ private void ReadNumberOrDate(char32? signPrefix = null, TextPosition? signPrefi
_token = new SyntaxTokenValue(TokenKind.Invalid, start, end);
return;
}
ReadDigits(ref end);
ReadDigits(ref end, false);
}

var numberAsText = _textBuilder.ToString();
Expand Down Expand Up @@ -646,10 +646,9 @@ private void ReadNumberOrDate(char32? signPrefix = null, TextPosition? signPrefi
_token = new SyntaxTokenValue(isFloat ? TokenKind.Float : TokenKind.Integer, start, end, resolvedValue);
}

private void ReadDigits(ref TextPosition end)
private void ReadDigits(ref TextPosition end, bool isPreviousDigit)
{
bool isDigit;
bool isPreviousDigit = CharHelper.IsDigit(_c);
while ((isDigit = CharHelper.IsDigit(_c)) || _c == '_')
{
if (isDigit)
Expand All @@ -671,7 +670,7 @@ private void ReadDigits(ref TextPosition end)

if (!isPreviousDigit)
{
AddError("An underscore `_` must not follow a digit", _position, _position);
AddError("An underscore `_` must not followed a digit", _position, _position);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/SharpToml/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal partial class Parser<TSourceView> where TSourceView : ISourceView
private SyntaxTokenValue _token;
private bool _hideNewLine;
private readonly List<SyntaxTrivia> _currentTrivias;
private TableSyntax _currentTable = null;
private TableSyntaxBase _currentTable = null;
private DiagnosticsBag _diagnostics;

/// <summary>
Expand Down Expand Up @@ -48,19 +48,19 @@ public DocumentSyntax Run()
{
if (itemEntry == null) continue;

if (itemEntry is TableSyntax table)
if (itemEntry is TableSyntaxBase table)
{
_currentTable = table;
AddToListAndUpdateSpan(doc.Entries, itemEntry);
AddToListAndUpdateSpan(doc.Items, itemEntry);
}
else if (_currentTable == null)
{
AddToListAndUpdateSpan(doc.Entries, itemEntry);
AddToListAndUpdateSpan(doc.Items, itemEntry);
}
else
{
// Otherwise, we know that we can only have a key-value
AddToListAndUpdateSpan(_currentTable.KeyValues, (KeyValueSyntax)itemEntry);
AddToListAndUpdateSpan(_currentTable.Items, (KeyValueSyntax)itemEntry);
}
}

Expand Down Expand Up @@ -361,7 +361,7 @@ private InlineTableSyntax ParseInlineTable()
return Close(inlineTable);
}

private TableSyntax ParseTableOrTableArray()
private TableSyntaxBase ParseTableOrTableArray()
{
// If we have a pending table, close it
if (_currentTable != null)
Expand All @@ -372,7 +372,7 @@ private TableSyntax ParseTableOrTableArray()

var previousState = _hideNewLine;
_hideNewLine = false;
var table = Open<TableSyntax>();
var table = isTableArray ? (TableSyntaxBase)Open<TableArraySyntax>() : Open<TableSyntax>();
try
{
table.OpenBracket = EatToken();
Expand Down
5 changes: 3 additions & 2 deletions src/SharpToml/SharpToml.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.0;net471</TargetFrameworks>
<TargetFrameworks>netstandard1.3</TargetFrameworks>
<!--<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.0;net471</TargetFrameworks>-->
<LangVersion>7.3</LangVersion>
<Description>SharpToml is a TOML parser library for .NET+Core with support for loading/saving while preserving perfect roundtrip</Description>
<Copyright>Alexandre Mutel</Copyright>
Expand Down
10 changes: 7 additions & 3 deletions src/SharpToml/Syntax/ArrayItemSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
namespace SharpToml.Syntax
Expand All @@ -8,6 +8,10 @@ public sealed class ArrayItemSyntax : SyntaxNode
private ValueSyntax _value;
private SyntaxToken _comma;

public ArrayItemSyntax() : base(SyntaxKind.ArrayItem)
{
}

public ValueSyntax Value
{
get => _value;
Expand All @@ -20,9 +24,9 @@ public SyntaxToken Comma
set => ParentToThis(ref _comma, value, TokenKind.Comma);
}

public override void Visit(ISyntaxVisitor visitor)
public override void Accept(SyntaxVisitor visitor)
{
visitor.Accept(this);
visitor.Visit(this);
}

public override int ChildrenCount => 2;
Expand Down
8 changes: 4 additions & 4 deletions src/SharpToml/Syntax/ArraySyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
namespace SharpToml.Syntax
Expand All @@ -8,7 +8,7 @@ public sealed class ArraySyntax : ValueSyntax
private SyntaxToken _openBracket;
private SyntaxToken _closeBracket;

public ArraySyntax()
public ArraySyntax() : base(SyntaxKind.Array)
{
Items = new SyntaxList<ArrayItemSyntax>() { Parent = this };
}
Expand All @@ -27,9 +27,9 @@ public SyntaxToken CloseBracket
set => ParentToThis(ref _closeBracket, value, TokenKind.CloseBracket);
}

public override void Visit(ISyntaxVisitor visitor)
public override void Accept(SyntaxVisitor visitor)
{
visitor.Accept(this);
visitor.Visit(this);
}

public override int ChildrenCount => 3;
Expand Down
10 changes: 7 additions & 3 deletions src/SharpToml/Syntax/BasicKeySyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
namespace SharpToml.Syntax
Expand All @@ -7,6 +7,10 @@ public sealed class BasicKeySyntax : BasicValueSyntax
{
private SyntaxToken _key;

public BasicKeySyntax() : base(SyntaxKind.BasicKey)
{
}

public SyntaxToken Key
{
get => _key;
Expand All @@ -15,9 +19,9 @@ public SyntaxToken Key

public override int ChildrenCount => 1;

public override void Visit(ISyntaxVisitor visitor)
public override void Accept(SyntaxVisitor visitor)
{
visitor.Accept(this);
visitor.Visit(this);
}

protected override SyntaxNode GetChildrenImpl(int index)
Expand Down
5 changes: 4 additions & 1 deletion src/SharpToml/Syntax/BasicValueSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
namespace SharpToml.Syntax
{
public abstract class BasicValueSyntax : ValueSyntax
{
protected BasicValueSyntax(SyntaxKind kind) : base(kind)
{
}
}
}
10 changes: 7 additions & 3 deletions src/SharpToml/Syntax/BooleanValueSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
namespace SharpToml.Syntax
Expand All @@ -8,6 +8,10 @@ public sealed class BooleanValueSyntax : ValueSyntax
private SyntaxToken _token;
private bool _value;

public BooleanValueSyntax() : base(SyntaxKind.Boolean)
{
}

public SyntaxToken Token
{
get => _token;
Expand Down Expand Up @@ -44,9 +48,9 @@ public bool Value
}
}

public override void Visit(ISyntaxVisitor visitor)
public override void Accept(SyntaxVisitor visitor)
{
visitor.Accept(this);
visitor.Visit(this);
}

public override int ChildrenCount => 1;
Expand Down
10 changes: 7 additions & 3 deletions src/SharpToml/Syntax/DateTimeValueSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
using System;
Expand All @@ -7,9 +7,13 @@ namespace SharpToml.Syntax
{
public sealed class DateTimeValueSyntax : ValueSyntax
{
public override void Visit(ISyntaxVisitor visitor)
public DateTimeValueSyntax() : base(SyntaxKind.DateTime)
{
visitor.Accept(this);
}

public override void Accept(SyntaxVisitor visitor)
{
visitor.Visit(this);
}

public SyntaxToken Token { get; set; }
Expand Down
10 changes: 10 additions & 0 deletions src/SharpToml/Syntax/DiagnosticsBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public void Clear()
HasErrors = false;
}

public void Warning(SourceSpan span, string text)
{
Add(new DiagnosticMessage(DiagnosticMessageKind.Warning, span, text));
}

public void Error(SourceSpan span, string text)
{
Add(new DiagnosticMessage(DiagnosticMessageKind.Error, span, text));
}

public List<DiagnosticMessage>.Enumerator GetEnumerator()
{
return _messages.GetEnumerator();
Expand Down
12 changes: 6 additions & 6 deletions src/SharpToml/Syntax/DocumentSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ namespace SharpToml.Syntax
{
public sealed class DocumentSyntax : SyntaxNode
{
public DocumentSyntax()
public DocumentSyntax() : base(SyntaxKind.Document)
{
Entries = new SyntaxList<TableEntrySyntax>() { Parent = this };
Items = new SyntaxList<TableEntrySyntax>() { Parent = this };
Diagnostics = new DiagnosticsBag();
}
public DiagnosticsBag Diagnostics { get; }

public bool HasErrors => Diagnostics.HasErrors;

public override void Visit(ISyntaxVisitor visitor)
public override void Accept(SyntaxVisitor visitor)
{
visitor.Accept(this);
visitor.Visit(this);
}

public SyntaxList<TableEntrySyntax> Entries { get; }
public SyntaxList<TableEntrySyntax> Items { get; }

public override int ChildrenCount => 1;

protected override SyntaxNode GetChildrenImpl(int index)
{
return Entries;
return Items;
}
}
}
10 changes: 7 additions & 3 deletions src/SharpToml/Syntax/DottedKeyItemSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
namespace SharpToml.Syntax
Expand All @@ -8,6 +8,10 @@ public sealed class DottedKeyItemSyntax : ValueSyntax
private SyntaxToken _dot;
private BasicValueSyntax _value;

public DottedKeyItemSyntax() : base(SyntaxKind.DottedKeyItem)
{
}

public SyntaxToken Dot
{
get => _dot;
Expand All @@ -20,9 +24,9 @@ public BasicValueSyntax Value
set => ParentToThis(ref _value, value);
}

public override void Visit(ISyntaxVisitor visitor)
public override void Accept(SyntaxVisitor visitor)
{
visitor.Accept(this);
visitor.Visit(this);
}

public override int ChildrenCount => 2;
Expand Down
10 changes: 7 additions & 3 deletions src/SharpToml/Syntax/FloatValueSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Copyright (c) 2019 - Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
namespace SharpToml.Syntax
Expand All @@ -7,6 +7,10 @@ public sealed class FloatValueSyntax : ValueSyntax
{
private SyntaxToken _token;

public FloatValueSyntax() : base(SyntaxKind.Float)
{
}

public SyntaxToken Token
{
get => _token;
Expand All @@ -15,9 +19,9 @@ public SyntaxToken Token

public double Value { get; set; }

public override void Visit(ISyntaxVisitor visitor)
public override void Accept(SyntaxVisitor visitor)
{
visitor.Accept(this);
visitor.Visit(this);
}

public override int ChildrenCount => 1;
Expand Down
Loading

0 comments on commit 4a766e4

Please sign in to comment.