}
public virtual string GetTokenTypeString (TokenType tokenType) => tokenType.ToString();
//protected abstract Tokenizer CreateTokenizer ();
- protected abstract SyntaxAnalyser CreateSyntaxAnalyser ();
public abstract IList GetSuggestions (int absoluteTextPos, int currentTokenIndex, SyntaxNode currentNode, CharLocation loc);
protected virtual async void parse () {
if (backgroundCompilationTask != null && !backgroundCompilationTask.IsCompleted) {
//CrowEditBase.App.Log (LogType.Low, $"Syntax Analysis done in {sw.ElapsedMilliseconds}(ms) {sw.ElapsedTicks}(ticks)");
}
+ protected abstract SyntaxAnalyser CreateSyntaxAnalyser ();
async void parseAssync(CancellationToken cancel) {
SyntaxAnalyser syntaxAnalyser = CreateSyntaxAnalyser ();
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using System.Collections.Generic;
-using System.Linq;
using System.Threading;
using System.Threading.Tasks;
-using Crow.Text;
namespace CrowEditBase
{
public abstract class SyntaxAnalyser {
- protected SourceDocument document;
+ #region CTOR
+ public SyntaxAnalyser (ReadOnlyTextBuffer source) {
+ this.source = source;
+ }
+ #endregion
+
+ protected ReadOnlyTextBuffer source;
protected SyntaxRootNode Root;
protected CancellationToken cancel;
public IEnumerable<SyntaxException> Exceptions => null;// Root?.GetAllExceptions();
- public SyntaxAnalyser (SourceDocument document) {
- this.document = document;
- }
public abstract Task<SyntaxRootNode> Process (CancellationToken cancel = default);
#region Token handling
if (msbp.IsCrowProject)
}*/
}
- protected override SyntaxAnalyser CreateSyntaxAnalyser() => new ImlSyntaxAnalyser (this);
+ protected override SyntaxAnalyser CreateSyntaxAnalyser() => new ImlSyntaxAnalyser (ImmutableBufferCopy);
public override string GetTokenTypeString (TokenType tokenType) => ((ImlTokenType)tokenType).ToString();
namespace CECrowPlugin
{
public class ImlSyntaxAnalyser : XmlSyntaxAnalyser {
- public ImlSyntaxAnalyser (ImlDocument document) : base (document) {}
+ public ImlSyntaxAnalyser (ReadOnlyTextBuffer document) : base (document) {}
public override async Task<SyntaxRootNode> Process (CancellationToken cancel = default) {
}*/
}
- protected override SyntaxAnalyser CreateSyntaxAnalyser() => new StyleSyntaxAnalyser (this);
+ protected override SyntaxAnalyser CreateSyntaxAnalyser() => new StyleSyntaxAnalyser (ImmutableBufferCopy);
public override IList GetSuggestions (int absoluteTextPos, int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) {
Token currentToken = GetTokenByIndex(currentTokenIndex);
public static bool Is(this Token tok, StyleTokenType type) => (StyleTokenType)tok.Type == type;
}
public class StyleSyntaxAnalyser : SyntaxAnalyser {
- public StyleSyntaxAnalyser (StyleDocument document) : base (document) {}
+ public StyleSyntaxAnalyser (ReadOnlyTextBuffer document) : base (document) {}
bool skipTriviaAndComments(MultiNodeSyntax currentNode) {
while (tryPeekFlag(out Token token, TokenType.Trivia)) {
public override async Task<SyntaxRootNode> Process (CancellationToken cancel = default) {
Tokenizer tokenizer = new StyleTokenizer();
- ReadOnlyTextBuffer buff = document.ImmutableBufferCopy;
- Token[] tokens = tokenizer.Tokenize(buff.Source.Span);
+ Token[] tokens = tokenizer.Tokenize(source.Source.Span);
tokIdx = 0;
this.cancel = cancel;
- Root = new StyleRootSyntax (buff, tokens);
+ Root = new StyleRootSyntax (source, tokens);
while (!EOF) {
if (cancel.IsCancellationRequested)
break;
public EbnfDocument (string fullPath, string editorPath) : base (fullPath, editorPath) {
}
- protected override SyntaxAnalyser CreateSyntaxAnalyser() => new EbnfSyntaxAnalyser (this);
+ protected override SyntaxAnalyser CreateSyntaxAnalyser() => new EbnfSyntaxAnalyser (ImmutableBufferCopy);
public override string GetTokenTypeString (TokenType tokenType) => ((EbnfTokenType)tokenType).ToString();
public override IList GetSuggestions (int absoluteTextPos, int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc) {
}
public class EbnfSyntaxAnalyser : SyntaxAnalyser {
- public EbnfSyntaxAnalyser (EbnfDocument document) : base (document) {}
+ public EbnfSyntaxAnalyser (ReadOnlyTextBuffer document) : base (document) {}
bool skipTriviaAndComments(MultiNodeSyntax currentNode) {
public override async Task<SyntaxRootNode> Process(CancellationToken cancel = default)
{
Tokenizer tokenizer = new EbnfTokenizer();
- ReadOnlyTextBuffer buff = document.ImmutableBufferCopy;
- Token[] tokens = tokenizer.Tokenize(buff.Source.Span);
+ Token[] tokens = tokenizer.Tokenize(source.Source.Span);
tokIdx = 0;
this.cancel = cancel;
- Root = new EbnfRootSyntax (buff, tokens);
+ Root = new EbnfRootSyntax (source, tokens);
/*while (!EOF) {
App.GetService<RoslynService> ()?.Start ();
}
- internal CSharpSyntaxTree tree;
- public CSDocument (string fullPath, string editorPath) : base (fullPath, editorPath) {
-
- tree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText (source.ToString(), CSharpParseOptions.Default);
- }
+ public CSDocument (string fullPath, string editorPath) : base (fullPath, editorPath) { }
#region SourceDocument abstract class implementation
- protected override SyntaxAnalyser CreateSyntaxAnalyser() => new CSSyntaxAnalyser (this);
+ protected override SyntaxAnalyser CreateSyntaxAnalyser() => new CSSyntaxAnalyser (ImmutableBufferCopy);
public override IList GetSuggestions (int absoluteTextPos, int currentTokenIndex, SyntaxNode CurrentNode, CharLocation loc)
{
}
#endregion
- /*public override Color GetColorForToken (TokenType tokType) {
- uint rawkind = (uint)tokType;
- uint tokCat = rawkind & 0xFF;
- CSTokenType cat = (CSTokenType)tokCat;
-
- SyntaxKind k = (SyntaxKind)tokType;
-
- //Console.WriteLine($"{k,50} {(((uint)tokType) ).ToString("B16") } {cat}");
-
- switch (cat) {
- case CSTokenType.Trivia:
- return Colors.Grey;
- case CSTokenType.Keyword:
- return Colors.DarkSlateBlue;
- default:
- return Colors.Black;
- }
- }*/
public override string GetTokenTypeString (TokenType tokenType) => ((SyntaxKind)tokenType).ToString();
public override Color GetColorForToken(Token token)
{
parse();
}
- protected override void parse()
- {
- tree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText (source.ToString(), CSharpParseOptions.Default, "", null);
- base.parse();
- }
}
}
\ No newline at end of file
}
CSTokenType convertTokenType(SyntaxKind kind) {
return (CSTokenType)kind;
- switch (kind) {
+ /*switch (kind) {
case SyntaxKind.None:
return CSTokenType.Unknown;
case SyntaxKind.List:
return CSTokenType.Unknown;
default:
return CSTokenType.Unknown;
- }
+ }*/
}
}
}
\ No newline at end of file
namespace CERoslynPlugin
{
public class CSSyntaxAnalyser : SyntaxAnalyser {
- CSDocument csdoc;
- public CSSyntaxAnalyser (CSDocument document) : base (document) {
- csdoc = document;
- }
+ public CSSyntaxAnalyser (ReadOnlyTextBuffer document) : base (document) { }
public override async Task<SyntaxRootNode> Process (CancellationToken cancel = default) {
- ReadOnlyTextBuffer buff = document.ImmutableBufferCopy;
- CsharpSyntaxWalkerBridge bridge = new CsharpSyntaxWalkerBridge(new CSRootSyntax (buff), cancel);
- CSharpSyntaxNode csroot = await csdoc.tree.GetRootAsync(cancel);
+ CSharpSyntaxTree tree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText (source.Source.Span.ToString(), CSharpParseOptions.Default, "", null);
+ CsharpSyntaxWalkerBridge bridge = new CsharpSyntaxWalkerBridge(new CSRootSyntax (source), cancel);
+ CSharpSyntaxNode csroot = await tree.GetRootAsync(cancel);
if (cancel.IsCancellationRequested)
return null;
public class XmlDocument : SourceDocument {
public XmlDocument (string fullPath, string editorPath) : base (fullPath, editorPath) { }
- protected override SyntaxAnalyser CreateSyntaxAnalyser() => new XmlSyntaxAnalyser (this);
+ protected override SyntaxAnalyser CreateSyntaxAnalyser() => new XmlSyntaxAnalyser (ImmutableBufferCopy);
public override string GetTokenTypeString (TokenType tokenType) => ((XmlTokenType)tokenType).ToString();
protected virtual IEnumerable<Suggestion> getElementNameSuggestions(string curName, TextChange change) => null;
public static bool Is(this Token tok, XmlTokenType type) => (XmlTokenType)tok.Type == type;
}
public class XmlSyntaxAnalyser : SyntaxAnalyser {
- public XmlSyntaxAnalyser (XmlDocument document) : base (document) {}
+ public XmlSyntaxAnalyser (ReadOnlyTextBuffer document) : base (document) {}
bool skipTriviaAndComments(MultiNodeSyntax currentNode, bool skipLineBreaks = true) {
while (tryPeekFlag(out Token token, TokenType.Trivia)) {
switch(token.GetTokenType()) {
}
return pi;
}
-
+ ElementSyntax processElement(ElementSyntax elt) {
+ while (!EOF) {
+ if (cancel.IsCancellationRequested)
+ break;
+ if (!skipTriviaAndComments(elt))
+ break;
+ if (Peek().Is(XmlTokenType.ElementOpen)) {
+ processElementNode(elt);
+ } else if (Peek().Is(XmlTokenType.EndElementOpen)) {
+ elt.AddChild(processNode(new ElementEndTagSyntax(Read())));
+ break;
+ } else if (Peek().Is(XmlTokenType.PI_Start)) {
+ elt.AddChild(processNode(new ProcessingInstructionSyntax(Read())));
+ } else {
+ elt.AddChild(new UnexpectedTokenSyntax(Read()));
+ }
+ }
+ return elt;
+ }
void processElementNode(MultiNodeSyntax node) {
ElementStartTagSyntax start = new ElementStartTagSyntax(Read());
if (accept (start, XmlTokenType.ElementName)) {
start.AddChild(new UnexpectedTokenSyntax(Read()));
node.AddChild(new ElementSyntax(start));
}
- }
- ElementSyntax processElement(ElementSyntax elt) {
- while (!EOF) {
- if (cancel.IsCancellationRequested)
- break;
- if (!skipTriviaAndComments(elt))
- break;
- if (Peek().Is(XmlTokenType.ElementOpen)) {
- processElementNode(elt);
- } else if (Peek().Is(XmlTokenType.EndElementOpen)) {
- elt.AddChild(processNode(new ElementEndTagSyntax(Read())));
- break;
- } else if (Peek().Is(XmlTokenType.PI_Start)) {
- elt.AddChild(processNode(new ProcessingInstructionSyntax(Read())));
- } else {
- elt.AddChild(new UnexpectedTokenSyntax(Read()));
- }
- }
- return elt;
- }
+ }
public override async Task<SyntaxRootNode> Process (CancellationToken cancel = default) {
Tokenizer tokenizer = new XmlTokenizer();
- ReadOnlyTextBuffer buff = document.ImmutableBufferCopy;
- Token[] tokens = tokenizer.Tokenize(buff.Source.Span);
+ Token[] tokens = tokenizer.Tokenize(source.Source.Span);
tokIdx = 0;
this.cancel = cancel;//?
- Root = new XMLRootSyntax (buff, tokens);
+ Root = new XMLRootSyntax (source, tokens);
while (!EOF) {
if (cancel.IsCancellationRequested)
break;
public class EmptyElementSyntax : MultiNodeSyntax {
public EmptyElementSyntax (ElementStartTagSyntax startNode) {
- AddChild (startNode);
+ foreach (var child in startNode.Children)
+ AddChild(child);
}
//public override bool IsComplete => base.IsComplete && StartTag != null;
}
BlockComment = 0x0105,
BlockCommentEnd = 0x0106,
Name = 0x0200,
- ElementName = 0x8201,
- AttributeName = 0x8202,
- PI_Target = 0x8203,
+ ElementName = 0x0201,
+ AttributeName = 0x0202,
+ PI_Target = 0x0203,
+ ConstantName = 0x0204,
Punctuation = 0x0400,
-
- PI_Start = 0x8401,// '<?'
- PI_End = 0x8402,// '?>'
- ElementOpen = 0x8403,// '<'
- EndElementOpen = 0x8404,// '</'
- EmptyElementClosing = 0x8405,// '/>'
- ClosingSign = 0x8406,// '>'
- DTDObjectOpen = 0x84A0,// '<!'
+ PI_Start = 0x0401,// '<?'
+ PI_End = 0x0402,// '?>'
+ ElementOpen = 0x0403,// '<'
+ EndElementOpen = 0x0404,// '</'
+ EmptyElementClosing = 0x0405,// '/>'
+ ClosingSign = 0x0406,// '>'
+ DTDObjectOpen = 0x04A0,// '<!'
Operator = 0x0800,
EqualSign = 0x0801,
Keyword = 0x1000,
- AttributeValue = 0x8000,
- AttributeValueOpen = 0x8401,
- AttributeValueClose = 0x8402,
+ AttributeValue = 0x2000,
+ AttributeValueOpen = 0x2001,
+ AttributeValueClose = 0x2002,
Content,