From 8e82014a444722660692bbf730017651ddb6c88c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Tue, 27 Feb 2018 19:21:36 +0100 Subject: [PATCH] change style grammar for value, editor parser ok --- CrowIDE/src/SourceEditor/Parser.cs | 43 +++------------ CrowIDE/src/SourceEditor/StyleParser.cs | 73 +++++++++++++------------ 2 files changed, 46 insertions(+), 70 deletions(-) diff --git a/CrowIDE/src/SourceEditor/Parser.cs b/CrowIDE/src/SourceEditor/Parser.cs index f3dced9b..f655fa49 100644 --- a/CrowIDE/src/SourceEditor/Parser.cs +++ b/CrowIDE/src/SourceEditor/Parser.cs @@ -198,17 +198,6 @@ namespace Crow.Coding currentTok = default(Token); } /// - /// Save current token into current TokensLine after having skipped white spaces and raz current token - /// - protected void saveAndResetAfterWhiteSpaceSkipping() { - buffer[currentLine].Tokens.Add (currentTok); - currentTok = default(Token); - if (WpToken == null) - return; - buffer[currentLine].Tokens.Add ((Token)WpToken); - WpToken = null; - } - /// /// read one char and add current token to current TokensLine, current token is reset /// /// Type of the token @@ -226,14 +215,6 @@ namespace Crow.Coding saveAndResetCurrentTok (); } /// - /// Save current tok after having skipped white spaces - /// - /// set the type of the tok - protected void saveAndResetAfterWhiteSpaceSkipping(System.Enum type) { - currentTok.Type = (TokenType)type; - saveAndResetAfterWhiteSpaceSkipping (); - } - /// /// Peek next char, emit '\n' if current column > buffer's line length /// Throw error if eof is true /// @@ -302,30 +283,20 @@ namespace Crow.Coding return tmp; } /// - /// skip white spaces, but not line break. Save spaces in a WhiteSpace token and dont - /// save it directely if currentTok is not null + /// skip white spaces, but not line break. Save spaces in a WhiteSpace token. /// protected void SkipWhiteSpaces () { - if (WpToken != null) - throw new ParsingException (this, "white space token already pending"); - Token wp = default(Token); + if (currentTok.Type != TokenType.Unknown) + throw new ParsingException (this, "current token should be reset to unknown (0) before skiping white spaces"); while (!eol) { if (!char.IsWhiteSpace (Peek ())||Peek()=='\n') break; - if (wp.Type == TokenType.Unknown) - wp.Start = CurrentPosition; - wp += Read(); - wp.Type = TokenType.WhiteSpace; + readToCurrTok (currentTok.Type == TokenType.Unknown); + currentTok.Type = TokenType.WhiteSpace; } - if (wp.Type == TokenType.Unknown) - return; - wp.End = CurrentPosition; - if (currentTok.Type == TokenType.Unknown) - buffer [currentLine].Tokens.Add (wp); - else - WpToken = wp; + if (currentTok.Type != TokenType.Unknown) + saveAndResetCurrentTok (); } - protected object WpToken = null; #endregion } } \ No newline at end of file diff --git a/CrowIDE/src/SourceEditor/StyleParser.cs b/CrowIDE/src/SourceEditor/StyleParser.cs index e699695d..9952b267 100644 --- a/CrowIDE/src/SourceEditor/StyleParser.cs +++ b/CrowIDE/src/SourceEditor/StyleParser.cs @@ -9,7 +9,7 @@ namespace Crow.Coding { public class StyleParser : Parser { - enum States { init, classNames, members } + enum States { init, classNames, members, value, endOfStatement } public StyleParser (CodeBuffer _buffer) : base(_buffer) { @@ -41,7 +41,6 @@ namespace Crow.Coding Debug.WriteLine (string.Format("parsing line:{0}", currentLine)); CodeLine cl = buffer [currentLine]; cl.Tokens = new List (); - WpToken = null; //retrieve current parser state from previous line if (currentLine > 0) @@ -80,62 +79,68 @@ namespace Crow.Coding } break; case ',': - if (currentTok.Type != TokenType.Identifier || curState == States.members ) + if (curState != States.init || curState != States.classNames ) throw new ParsingException (this, "Unexpected char ','"); - saveAndResetAfterWhiteSpaceSkipping (TokenType.Type);//save previous token as class - readToCurrTok (true); - saveAndResetCurrentTok (TokenType.UnaryOp); + readAndResetCurrentTok (TokenType.UnaryOp, true); curState = States.classNames; break; case '{': - if (currentTok.Type != TokenType.Identifier || curState == States.members) - throw new ParsingException (this, "Unexpected char '}'"); - - saveAndResetAfterWhiteSpaceSkipping (TokenType.Type);//save previous token as class - - readToCurrTok (true); - saveAndResetCurrentTok (TokenType.OpenBlock); + if (!(curState == States.init || curState == States.classNames)) + throw new ParsingException (this, "Unexpected char '{'"); + readAndResetCurrentTok (TokenType.OpenBlock, true); curState = States.members; break; case '}': if (curState != States.members) throw new ParsingException (this, "Unexpected char '}'"); - readToCurrTok (true); - saveAndResetCurrentTok (TokenType.CloseBlock); + readAndResetCurrentTok (TokenType.CloseBlock, true); curState = States.classNames; break; case '=': - if (currentTok.Type != TokenType.Identifier) + if (curState == States.classNames) throw new ParsingException (this, "Unexpected char '='"); - - saveAndResetAfterWhiteSpaceSkipping ();//save previous token as propertyname - - curState = States.members; - - readToCurrTok (true); - saveAndResetCurrentTok (TokenType.Affectation); - - SkipWhiteSpaces (); - - currentTok+=ReadLineUntil(";"); + readAndResetCurrentTok (TokenType.Affectation, true); + curState = States.value; + break; + case '"': + if (curState != States.value) + throw new ParsingException (this, "Unexpected char '\"'"); + readAndResetCurrentTok (TokenType.StringLitteralOpening, true); + + while (!eol) { + currentTok += ReadLineUntil ("\""); + if (currentTok.Content [currentTok.Content.Length - 1] == '\\') + readToCurrTok (); + else + break; + } + if (eol) + throw new ParsingException (this, "Unexpected end of line"); saveAndResetCurrentTok (TokenType.StringLitteral); - if (Peek() != ';') - throw new ParsingException (this, "Expecting ';'"); - readToCurrTok (true); - saveAndResetCurrentTok (TokenType.StatementEnding); + readAndResetCurrentTok (TokenType.StringLitteralClosing, true); + curState = States.endOfStatement; + break; + case ';': + if (curState != States.endOfStatement) + throw new ParsingException (this, "Unexpected end of statement"); + readAndResetCurrentTok (TokenType.StatementEnding, true); + curState = States.members; break; default: if (currentTok.Type != TokenType.Unknown) - throw new ParsingException (this, "error"); + throw new ParsingException (this, "error curtok not null"); + if (curState == States.value) + throw new ParsingException (this, "expecting value enclosed in '\"'"); + if (curState == States.endOfStatement) + throw new ParsingException (this, "expecting end of statement"); if (nextCharIsValidCharStartName) { readToCurrTok (true); while (nextCharIsValidCharName) readToCurrTok (); } - currentTok.Type = TokenType.Identifier; - currentTok.End = CurrentPosition; + saveAndResetCurrentTok (TokenType.Identifier); break; } } -- 2.47.3