Skip to content

Commit

Permalink
fixing things and adding more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Baudin999 committed Dec 18, 2019
1 parent b08b033 commit a04566b
Show file tree
Hide file tree
Showing 28 changed files with 649 additions and 100 deletions.
24 changes: 21 additions & 3 deletions CLI/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public int Watch()

// Add event handlers.
watcher.Changed += OnChanged;
//watcher.Created += OnCreate;
watcher.Created += OnCreate;
watcher.Deleted += OnDelete;
watcher.Renamed += OnRenamed;

Expand Down Expand Up @@ -195,11 +195,29 @@ private void OnChanged(object source, FileSystemEventArgs e)
}
}

private void OnCreate(object source, FileSystemEventArgs e)
{
try
{
var module = Modules.FirstOrDefault(m => m.Path == e.FullPath);
if (module is null)
{
Console.WriteLine($"{e.ChangeType}: {e.FullPath}");
module = new Module(e.FullPath, this.Path, this);
Modules.Add(module);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

private void OnDelete(object source, FileSystemEventArgs e)
{
try
{
Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");
Console.WriteLine($"{e.ChangeType}: {e.FullPath}");
Modules.Remove(Modules.FirstOrDefault(m => m.Path == e.FullPath));
}
catch (Exception ex)
Expand All @@ -212,7 +230,7 @@ private void OnRenamed(object source, RenamedEventArgs e)
{
try
{
Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}");
Console.WriteLine($"{e.ChangeType}: {e.OldFullPath} renamed to {e.FullPath}");
Modules.Remove(Modules.FirstOrDefault(m => m.Path == e.OldFullPath));

var module = new Module(e.FullPath, this.Path, this);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/AST/ASTAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Compiler.AST
{
public class ASTAlias : IASTNode, IRestrictable, IElement, INamable, IRootNode, ICloneable
public class ASTAlias : IASTNode, IRestrictable, IElement, ITypeble, INamable, IRootNode, ICloneable
{
public ASTName ASTName { get; }
public string Name { get { return this.ASTName.Name; } }
Expand Down
2 changes: 1 addition & 1 deletion Compiler/AST/ASTTypeField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Compiler.AST
{
public class ASTTypeField : IASTNode, IRestrictable, IElement, INamable, ICloneable, IEqualityComparer<ASTTypeField>
public class ASTTypeField : IASTNode, IRestrictable, IElement, ITypeble, INamable, ICloneable, IEqualityComparer<ASTTypeField>
{
public Token? Token { get; } = Token.Empty();
public ASTName ASTName { get; }
Expand Down
6 changes: 5 additions & 1 deletion Compiler/AST/IElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ namespace Compiler.AST
public interface IElement
{
public ASTName ASTName { get; }
public IEnumerable<ASTTypeDefinition> Types { get; }
}

public interface ITypeble
{
public IEnumerable<ASTTypeDefinition> Types { get; }
}
}
2 changes: 1 addition & 1 deletion Compiler/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public IEnumerable<Token> Lex(string code)
}
else if (Char2.Indent(input.Current))
{
yield return TokenLexers.Take(input, TokenType.Indent);
yield return TokenLexers.TakeIndent(input);
}
else if (Char2.Equal(input.Current))
{
Expand Down
20 changes: 12 additions & 8 deletions Compiler/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Parser(IEnumerable<Token> tokenStream)
public Token Peek(int index = 1) => tokenStream[position + index];

public static Parser Empty() => new Parser();
public static string[] BaseTypes = { "String", "Number", "Boolean", "Date", "Time", "DateTime" };
public static string[] BaseTypes = { "String", "Number", "Decimal", "Boolean", "Date", "Time", "DateTime" };

public IEnumerable<IASTNode> Parse(string moduleName = "")
{
Expand Down Expand Up @@ -150,6 +150,7 @@ public IEnumerable<IASTNode> Parse(string moduleName = "")
/// <returns></returns>
public Token Consume(TokenType tokenType, bool ignoreWhitespace = true)
{

while (true)
{
if (this.Current.TokenType == tokenType)
Expand All @@ -164,25 +165,28 @@ public Token Consume(TokenType tokenType, bool ignoreWhitespace = true)
}
else
{
var previous1 = this.TryTake(-2)?.Value ?? "";
var previous = this.TryTake()?.Value ?? "";
var value = this.TryTake(0)?.Value ?? "";
var next = this.TryTake(1)?.Value ?? "";
var next1 = this.TryTake(2)?.Value ?? "";
var parts = new List<string?>();
for (var i = -10; i < 11; ++i)
{
parts.Add(this.TryTake(i)?.Value);
}
var s = string.Join("", parts.OfType<string>().ToList());
var value = this.Current.Value;

var message = tokenType switch
{
TokenType.Identifier => $@"
Expected an Identifier but found a {this.Current.TokenType}: '{value}'.
Line {this.Current.StartLine}, Column {this.Current.StartColumn}
...{previous1} {previous} {value} {next} {next1}...
...{s}...
In ZDragon we expect types to be represented by an Identifier and
identifiers always start with a capital letter and have no spaces
or other symbols.
",
TokenType.EndStatement => $@"
Expected an Enstatement but found a {this.Current.TokenType}: '{value}'.
...{previous1} {previous} {value} {next} {next1}...
...{s}...
",
_ => $"Invalid Token: Expected a {tokenType} but found {this.Current.TokenType} on line {this.Current.StartLine} and column {this.Current.StartColumn}"
};
Expand Down
7 changes: 4 additions & 3 deletions Compiler/Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Resolver(IEnumerable<IASTNode> ast)
{
var (resolve_alias_errors, resolvedAlias) = ResolveAlias(alias);
errors.AddRange(resolve_alias_errors);
nodes.Add(resolvedAlias);
if (resolvedAlias != null) nodes.Add(resolvedAlias);
}
else if (node is ASTType t)
{
Expand Down Expand Up @@ -104,7 +104,7 @@ public Resolver(IEnumerable<IASTNode> ast)
}
}

private (IEnumerable<IASTError>, IASTNode) ResolveAlias(ASTAlias alias)
private (IEnumerable<IASTError>, IASTNode?) ResolveAlias(ASTAlias alias)
{
// here we'll resolve generic aliasses
var _mod = alias.Types.First().Value;
Expand All @@ -131,7 +131,8 @@ public Resolver(IEnumerable<IASTNode> ast)
}
else if (source is ASTView)
{
return (errors, (IASTNode)((ASTView)source).Clone(alias.Name));
return (errors, null);
//return (errors, (IASTNode)((ASTView)source).Clone(alias.Name));
}
else if (source is ASTType type)
{
Expand Down
23 changes: 23 additions & 0 deletions Compiler/Tokens/Lexer.Indent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
namespace Compiler
{
public static partial class TokenLexers
{
internal static Token TakeIndent(Input input)
{
input.Next();

return new Token()
{
StartIndex = input.Position - 1,
StartColumn = input.Column - 1,
StartLine = input.Line - 1,
EndIndex = input.Position,
EndColumn = 0,
EndLine = input.Line,
Value = " ",
TokenType = TokenType.NewLine
};
}
}
}
23 changes: 23 additions & 0 deletions Compiler/Tokens/Lexer.NewLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
namespace Compiler
{
public static partial class TokenLexers
{
internal static Token TakeNewLine(Input input)
{
var result = new Token()
{
StartIndex = input.Position,
StartColumn = input.Column,
StartLine = input.Line,
EndIndex = input.Position + 1,
EndColumn = 0,
EndLine = input.Line + 1,
Value = Environment.NewLine,
TokenType = TokenType.NewLine
};
input.Next();
return result;
}
}
}
34 changes: 0 additions & 34 deletions Compiler/Tokens/Lexer.Tokeniser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,6 @@ internal static Token Take(Input input, TokenType type)
return result;
}

internal static Token TakeNewLine(Input input)
{
var result = new Token()
{
StartIndex = input.Position,
StartColumn = input.Column,
StartLine = input.Line,
EndIndex = input.Position + 1,
EndColumn = 0,
EndLine = input.Line + 1,
Value = Environment.NewLine,
TokenType = TokenType.NewLine
};
input.Next();
return result;
}

//internal static Token TakeIndent(Input input)
//{
// input.Next();
// return new Token()
// {
// StartIndex = input.Position - 1,
// StartColumn = input.Column - 1,
// StartLine = input.Line - 1,
// EndIndex = input.Position,
// EndColumn = 0,
// EndLine = input.Line,
// Value = Environment.NewLine,
// TokenType = TokenType.NewLine
// };
//}


}

public static class Char2
Expand Down
4 changes: 2 additions & 2 deletions CompilerTests/ParserTests_Alias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ public void AliasOfAView()

Assert.NotNull(school);
Assert.NotNull(schoolView);
Assert.NotNull(anotherShoolView);
Assert.Null(anotherShoolView);

Assert.Equal(3, g.AST.Count);
Assert.Equal(2, g.AST.Count);
}
}
}
2 changes: 2 additions & 0 deletions Mapper.HTML/MermaidMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public override string VisitASTData(ASTData astData)
var _type = f.Name;
if (_type != "String"
&& _type != "Number"
&& _type != "Decimal"
&& _type != "Boolean"
&& _type != "Date"
&& _type != "DateTime"
Expand Down Expand Up @@ -76,6 +77,7 @@ public override string VisitASTType(ASTType astType)
var _type = f.Types.Last().Value;
if (_type != "String"
&& _type != "Number"
&& _type != "Decimal"
&& _type != "Boolean"
&& _type != "Date"
&& _type != "DateTime"
Expand Down
Loading

0 comments on commit a04566b

Please sign in to comment.