Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5ba6c0d
Added less action
dkleszyk Feb 10, 2021
6a69d46
Merge branch 'master' into 212-a-new-lexer-command-less
dkleszyk Feb 10, 2021
f7c604e
Added untracked files
dkleszyk Feb 10, 2021
1b8c1b5
Fixed empty files
dkleszyk Feb 10, 2021
3ce4e3b
Merge branch 'master' into 212-a-new-lexer-command-less
dkleszyk Feb 19, 2021
b2813ed
Updated contributors.txt
dkleszyk Feb 19, 2021
5838f45
Added documentation for 'less' command
dkleszyk Feb 19, 2021
bce0b91
Added test for 'less' action
dkleszyk Feb 19, 2021
ba27209
Fixed lexer grammar for 'less' command test
dkleszyk Feb 19, 2021
2ad1477
Fixed token rewind for 'less' action
dkleszyk Feb 19, 2021
4366ccc
Fixed indent
dkleszyk Feb 19, 2021
f521db7
Use built-ins for resetting line and line position
dkleszyk Feb 19, 2021
505cc58
Add lexer methods for setting line and line position
dkleszyk Feb 19, 2021
e91e700
Fixed typo setting line position
dkleszyk Feb 20, 2021
60cdbfe
Merge branch 'master' into 212-a-new-lexer-command-less
dkleszyk Mar 2, 2021
6354cff
Merge branch 'master' into 212-a-new-lexer-command-less
dkleszyk Apr 8, 2021
0a78c30
Fixed typos in documentation
dkleszyk Apr 8, 2021
8f247bc
Added missing token type definition for javascript target
dkleszyk Apr 9, 2021
89f15fe
Updated author and version
dkleszyk Apr 9, 2021
1c7f4de
Updated copyright year on touched files
dkleszyk Apr 9, 2021
13a872c
Merge branch 'master' into 212-a-new-lexer-command-less
dkleszyk Apr 15, 2021
4a32e00
Merge branch 'master' into 212-a-new-lexer-command-less
dkleszyk May 18, 2021
c6429b5
Merge branch 'master' into 212-a-new-lexer-command-less
dkleszyk Jun 28, 2021
6cb910e
Merge branch 'master' into 212-a-new-lexer-command-less
dkleszyk Nov 7, 2021
e434105
Removed name accidentally pulled in from another merge
dkleszyk Nov 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ YYYY/MM/DD, github id, Full name, email
2021/01/25, l215884529, Qiheng Liu, 13607681+l215884529@users.noreply.github.com
2021/02/02, tsotnikov, Taras Sotnikov, taras.sotnikov@gmail.com
2021/02/10, jirislaby, Jiri Slaby, jirislaby@gmail.com
2021/02/19, dkleszyk, David Kleszyk, dkleszyk@gmail.com
2021/02/21, namasikanam, Xingyu Xie, namasikanam@gmail.com
2021/02/26, ahooper, Andrew Hooper, ahooper at kos dot net
2021/02/27, khmarbaise, Karl Heinz Marbaise, github@soebes.com
Expand Down
21 changes: 21 additions & 0 deletions doc/lexer-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ An alternative can have more than one command separated by commas. Here are the

* skip
* more
* less
* popMode
* mode( x )
* pushMode( x )
Expand Down Expand Up @@ -271,6 +272,26 @@ TEXT : . -> more ; // collect more text for string

Popping the bottom layer of a mode stack will result in an exception. Switching modes with `mode` changes the current stack top. More than one `more` is the same as just one and the position does not matter.

### less

The 'less' is similar to the 'more' command; it forces the lexer to get another token without throwing out the current text. However, the 'less' command will also rewind the lexer position to the start of the token before resuming.

As a comparison:

More:

```
// When the PROC_INSTR mode is entered, the lexer will continue matching after the "Name"
SPECIAL_OPEN : '<?' Name -> more, pushMode(PROC_INSTR);
```

Less:

```
// When the PROC_INSTR mode is entered, the lexer will restart matching at the opening tag ("<?")
SPECIAL_OPEN : '<?' Name -> less, pushMode(PROC_INSTR);
```

### type()

```
Expand Down
7 changes: 6 additions & 1 deletion runtime/CSharp/src/Atn/ATNDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand Down Expand Up @@ -1284,6 +1284,11 @@ protected internal virtual ILexerAction LexerActionFactory(LexerActionType type,
return new LexerCustomAction(data1, data2);
}

case LexerActionType.Less:
{
return LexerLessAction.Instance;
}

case LexerActionType.Mode:
{
return new LexerModeAction(data1);
Expand Down
6 changes: 4 additions & 2 deletions runtime/CSharp/src/Atn/LexerActionType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand All @@ -18,6 +18,8 @@ public enum LexerActionType
PopMode,
PushMode,
Skip,
Type
Type,
/// <since>4.9.3</since>
Less
}
}
98 changes: 98 additions & 0 deletions runtime/CSharp/src/Atn/LexerLessAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
using Antlr4.Runtime;
using Antlr4.Runtime.Atn;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;

namespace Antlr4.Runtime.Atn
{
/// <summary>
/// Implements the
/// <c>less</c>
/// lexer action by calling
/// <see cref="Antlr4.Runtime.Lexer.Less()"/>
/// .
/// <p>The
/// <c>less</c>
/// command does not have any parameters, so this action is
/// implemented as a singleton instance exposed by
/// <see cref="Instance"/>
/// .</p>
/// </summary>
/// <author>David Kleszyk</author>
/// <since>4.9.3</since>
public sealed class LexerLessAction : ILexerAction
{
/// <summary>Provides a singleton instance of this parameterless lexer action.</summary>
/// <remarks>Provides a singleton instance of this parameterless lexer action.</remarks>
public static readonly Antlr4.Runtime.Atn.LexerLessAction Instance = new Antlr4.Runtime.Atn.LexerLessAction();

/// <summary>
/// Constructs the singleton instance of the lexer
/// <c>less</c>
/// command.
/// </summary>
private LexerLessAction()
{
}

/// <summary><inheritDoc/></summary>
/// <returns>
/// This method returns
/// <see cref="LexerActionType.Less"/>
/// .
/// </returns>
public LexerActionType ActionType
{
get
{
return LexerActionType.Less;
}
}

/// <summary><inheritDoc/></summary>
/// <returns>
/// This method returns
/// <see langword="false"/>
/// .
/// </returns>
public bool IsPositionDependent
{
get
{
return false;
}
}

/// <summary>
/// <inheritDoc/>
/// <p>This action is implemented by calling
/// <see cref="Antlr4.Runtime.Lexer.Less()"/>
/// .</p>
/// </summary>
public void Execute(Lexer lexer)
{
lexer.Less();
}

public override int GetHashCode()
{
int hash = MurmurHash.Initialize();
hash = MurmurHash.Update(hash, (int)(ActionType));
return MurmurHash.Finish(hash, 1);
}

public override bool Equals(object obj)
{
return obj == this;
}

public override string ToString()
{
return "less";
}
}
}
15 changes: 13 additions & 2 deletions runtime/CSharp/src/Lexer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand Down Expand Up @@ -192,8 +192,14 @@ public virtual IToken NextToken()
{
goto outer_continue;
}
if (_type == TokenTypes.Less)
{
_input.Seek(_tokenStartCharIndex);
this.Column = _tokenStartColumn;
this.Line = _tokenStartLine;
}
}
while (_type == TokenTypes.More);
while (_type == TokenTypes.More || _type == TokenTypes.Less);
if (_token == null)
{
Emit();
Expand Down Expand Up @@ -231,6 +237,11 @@ public virtual void More()
_type = TokenTypes.More;
}

public virtual void Less()
{
_type = TokenTypes.Less;
}

public virtual void Mode(int m)
{
_mode = m;
Expand Down
4 changes: 3 additions & 1 deletion runtime/CSharp/src/TokenTypes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand All @@ -9,5 +9,7 @@ public static class TokenTypes
public const int More = -2;

public const int Skip = -3;

public const int Less = -4;
}
}
13 changes: 11 additions & 2 deletions runtime/Cpp/runtime/src/Lexer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand Down Expand Up @@ -93,7 +93,12 @@ std::unique_ptr<Token> Lexer::nextToken() {
if (type == SKIP) {
goto outerContinue;
}
} while (type == MORE);
if (type == LESS) {
_input->seek(tokenStartCharIndex);
setCharPositionInLine(tokenStartCharPositionInLine);
setLine(tokenStartLine);
}
} while (type == MORE || type == LESS);
if (token == nullptr) {
emit();
}
Expand All @@ -109,6 +114,10 @@ void Lexer::more() {
type = MORE;
}

void Lexer::less() {
type = LESS;
}

void Lexer::setMode(size_t m) {
mode = m;
}
Expand Down
5 changes: 4 additions & 1 deletion runtime/Cpp/runtime/src/Lexer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand All @@ -22,6 +22,7 @@ namespace antlr4 {
static constexpr size_t DEFAULT_MODE = 0;
static constexpr size_t MORE = std::numeric_limits<size_t>::max() - 1;
static constexpr size_t SKIP = std::numeric_limits<size_t>::max() - 2;
static constexpr size_t LESS = std::numeric_limits<size_t>::max() - 3;

static constexpr size_t DEFAULT_TOKEN_CHANNEL = Token::DEFAULT_CHANNEL;
static constexpr size_t HIDDEN = Token::HIDDEN_CHANNEL;
Expand All @@ -32,6 +33,7 @@ namespace antlr4 {
DEFAULT_MODE = 0,
MORE = static_cast<size_t>(-2), // std::numeric_limits<size_t>::max() - 1; doesn't work in VS 2013
SKIP = static_cast<size_t>(-3), // std::numeric_limits<size_t>::max() - 2; doesn't work in VS 2013
LESS = static_cast<size_t>(-4), // std::numeric_limits<size_t>::max() - 3; doesn't work in VS 2013

DEFAULT_TOKEN_CHANNEL = Token::DEFAULT_CHANNEL,
HIDDEN = Token::HIDDEN_CHANNEL,
Expand Down Expand Up @@ -104,6 +106,7 @@ namespace antlr4 {
/// and emits it.
virtual void skip();
virtual void more();
virtual void less();
virtual void setMode(size_t m);
virtual void pushMode(size_t m);
virtual size_t popMode();
Expand Down
3 changes: 2 additions & 1 deletion runtime/Cpp/runtime/src/antlr4-runtime.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand Down Expand Up @@ -86,6 +86,7 @@
#include "atn/LexerChannelAction.h"
#include "atn/LexerCustomAction.h"
#include "atn/LexerIndexedCustomAction.h"
#include "atn/LexerLessAction.h"
#include "atn/LexerModeAction.h"
#include "atn/LexerMoreAction.h"
#include "atn/LexerPopModeAction.h"
Expand Down
6 changes: 5 additions & 1 deletion runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand Down Expand Up @@ -42,6 +42,7 @@

#include "atn/LexerCustomAction.h"
#include "atn/LexerChannelAction.h"
#include "atn/LexerLessAction.h"
#include "atn/LexerModeAction.h"
#include "atn/LexerMoreAction.h"
#include "atn/LexerPopModeAction.h"
Expand Down Expand Up @@ -731,6 +732,9 @@ Ref<LexerAction> ATNDeserializer::lexerActionFactory(LexerActionType type, int d
case LexerActionType::CUSTOM:
return std::make_shared<LexerCustomAction>(data1, data2);

case LexerActionType::LESS:
return LexerLessAction::getInstance();

case LexerActionType::MODE:
return std::make_shared< LexerModeAction>(data1);

Expand Down
7 changes: 6 additions & 1 deletion runtime/Cpp/runtime/src/atn/ATNSerializer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand Down Expand Up @@ -307,6 +307,11 @@ std::vector<size_t> ATNSerializer::serialize() {
break;
}

case LexerActionType::LESS:
data.push_back(0);
data.push_back(0);
break;

case LexerActionType::MODE:
{
int mode = std::dynamic_pointer_cast<LexerModeAction>(action)->getMode();
Expand Down
8 changes: 7 additions & 1 deletion runtime/Cpp/runtime/src/atn/LexerActionType.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* Copyright (c) 2012-2021 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
Expand Down Expand Up @@ -49,6 +49,12 @@ namespace atn {
/// The type of a <seealso cref="LexerTypeAction"/> action.
/// </summary>
TYPE,
/// <summary>
/// The type of a <seealso cref="LexerLessAction"/> action.
///
/// @since 4.9.3
/// </summary>
LESS,
};

} // namespace atn
Expand Down
Loading