Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev' into fix-nodenext-module-…
Browse files Browse the repository at this point in the history
…resolution

; Conflicts:
;	runtime/JavaScript/package.json
  • Loading branch information
KurtGokhan committed Jul 26, 2024
2 parents 67244f2 + 88a0c7a commit b77cb52
Show file tree
Hide file tree
Showing 48 changed files with 718 additions and 189 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/hosted.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
fail-fast: false
matrix:
os: [
macos-11,
macos-12,
ubuntu-20.04,
windows-2022
]
Expand Down Expand Up @@ -160,7 +160,7 @@ jobs:
fail-fast: false
matrix:
os: [
macos-11,
macos-12,
ubuntu-20.04,
windows-2022
]
Expand Down Expand Up @@ -196,6 +196,14 @@ jobs:
repository: antlr/antlr-php-runtime
path: runtime/PHP

- name: Setup PHP 8.2
if: matrix.target == 'php'
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: mbstring
tools: composer

- name: Install dependencies
env:
COMPOSER_CACHE_DIR: ${{ github.workspace }}/.cache
Expand Down Expand Up @@ -263,14 +271,6 @@ jobs:
with:
go-version: '^1.19'

- name: Setup PHP 8.2
if: matrix.target == 'php'
uses: shivammathur/setup-php@2.22.0
with:
php-version: '8.2'
extensions: mbstring
tools: composer

- name: Setup Swift
if: matrix.target == 'swift'
uses: swift-actions/setup-swift@v1.19.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ You might also find the following pages useful, particularly if you want to mess

Programmers run into parsing problems all the time. Whether it’s a data format like JSON, a network protocol like SMTP, a server configuration file for Apache, a PostScript/PDF file, or a simple spreadsheet macro language—ANTLR v4 and this book will demystify the process. ANTLR v4 has been rewritten from scratch to make it easier than ever to build parsers and the language applications built on top. This completely rewritten new edition of the bestselling Definitive ANTLR Reference shows you how to take advantage of these new features.

You can buy the book [The Definitive ANTLR 4 Reference](http://amzn.com/1934356999) at amazon or an [electronic version at the publisher's site](https://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference).
You can buy the book [The Definitive ANTLR 4 Reference](http://amzn.com/dp/1934356999) at amazon or an [electronic version at the publisher's site](https://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference).

You will find the [Book source code](http://pragprog.com/titles/tpantlr2/source_code) useful.

Expand Down
2 changes: 1 addition & 1 deletion doc/cpp-target.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# C++

The C++ target supports all platforms that can either run MS Visual Studio 2013 (or newer), XCode 7 (or newer) or CMake (C++11 required). All build tools can either create static or dynamic libraries, both as 64bit or 32bit arch. Additionally, XCode can create an iOS library. Also see [Antlr4 for C++ with CMake: A practical example](http://blorente.me/beyond-the-loop/Antlr-cpp-cmake/).
The C++ target supports all platforms that can either run MS Visual Studio 2017 (or newer), XCode 7 (or newer) or CMake (C++17 required). All build tools can either create static or dynamic libraries, both as 64bit or 32bit arch. Additionally, XCode can create an iOS library. Also see [Antlr4 for C++ with CMake: A practical example](http://blorente.me/beyond-the-loop/Antlr-cpp-cmake/).

## How to create a C++ lexer or parser?
This is pretty much the same as creating a Java lexer or parser, except you need to specify the language target, for example:
Expand Down
2 changes: 0 additions & 2 deletions doc/javascript-target.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ Once you've generated the lexer and/or parser code, you need to download the run

The JavaScript runtime is [available from npm](https://www.npmjs.com/package/antlr4).

If you can't use npm, the JavaScript runtime is also available from the ANTLR web site [download section](http://www.antlr.org/download/index.html). The runtime is provided in the form of source code, so no additional installation is required.

We will not document here how to refer to the runtime from your project, since this would differ a lot depending on your project type and IDE.

## How do I get the runtime in my browser?
Expand Down
12 changes: 10 additions & 2 deletions runtime/Cpp/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ if (NOT ANTLR_BUILD_SHARED AND NOT ANTLR_BUILD_STATIC)
message(FATAL_ERROR "Options ANTLR_BUILD_SHARED and ANTLR_BUILD_STATIC can't both be OFF")
endif()

include_directories(
set(libantlrcpp_INCLUDE_INSTALL_DIR "include/antlr4-runtime")

set(libantlrcpp_INCLUDE_DIRS
${PROJECT_SOURCE_DIR}/runtime/src
${PROJECT_SOURCE_DIR}/runtime/src/atn
${PROJECT_SOURCE_DIR}/runtime/src/dfa
Expand All @@ -34,9 +36,15 @@ file(GLOB libantlrcpp_SRC

if (ANTLR_BUILD_SHARED)
add_library(antlr4_shared SHARED ${libantlrcpp_SRC})
target_include_directories(antlr4_shared PUBLIC
"$<BUILD_INTERFACE:${libantlrcpp_INCLUDE_DIRS}>"
"$<INSTALL_INTERFACE:${libantlrcpp_INCLUDE_INSTALL_DIR}>")
endif()
if (ANTLR_BUILD_STATIC)
add_library(antlr4_static STATIC ${libantlrcpp_SRC})
target_include_directories(antlr4_static PUBLIC
"$<BUILD_INTERFACE:${libantlrcpp_INCLUDE_DIRS}>"
"$<INSTALL_INTERFACE:${libantlrcpp_INCLUDE_INSTALL_DIR}>")
endif()

if (CMAKE_HOST_UNIX)
Expand Down Expand Up @@ -185,7 +193,7 @@ if (TARGET antlr4_static)
endif()

install(DIRECTORY "${PROJECT_SOURCE_DIR}/runtime/src/"
DESTINATION "include/antlr4-runtime"
DESTINATION "${libantlrcpp_INCLUDE_INSTALL_DIR}"
COMPONENT dev
FILES_MATCHING PATTERN "*.h"
)
21 changes: 12 additions & 9 deletions runtime/Cpp/runtime/src/TokenStreamRewriter.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-2017 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 @@ -313,6 +313,10 @@ std::string TokenStreamRewriter::getText(const std::string &programName, const I
std::unordered_map<size_t, TokenStreamRewriter::RewriteOperation*> TokenStreamRewriter::reduceToSingleOperationPerIndex(
std::vector<TokenStreamRewriter::RewriteOperation*> &rewrites) {

// Reset the instructionIndex
for (size_t i = 0; i < rewrites.size(); ++i) {
rewrites[i]->instructionIndex = i;
}

// WALK REPLACES
for (size_t i = 0; i < rewrites.size(); ++i) {
Expand All @@ -327,35 +331,34 @@ std::unordered_map<size_t, TokenStreamRewriter::RewriteOperation*> TokenStreamRe
if (iop->index == rop->index) {
// E.g., insert before 2, delete 2..2; update replace
// text to include insert before, kill insert
delete rewrites[iop->instructionIndex];
rewrites[iop->instructionIndex] = nullptr;
rop->text = iop->text + (!rop->text.empty() ? rop->text : "");
rewrites[iop->instructionIndex] = nullptr;
delete iop;
}
else if (iop->index > rop->index && iop->index <= rop->lastIndex) {
// delete insert as it's a no-op.
delete rewrites[iop->instructionIndex];
rewrites[iop->instructionIndex] = nullptr;
delete iop;
}
}
// Drop any prior replaces contained within
std::vector<ReplaceOp*> prevReplaces = getKindOfOps<ReplaceOp>(rewrites, i);
for (auto *prevRop : prevReplaces) {
if (prevRop->index >= rop->index && prevRop->lastIndex <= rop->lastIndex) {
// delete replace as it's a no-op.
delete rewrites[prevRop->instructionIndex];
rewrites[prevRop->instructionIndex] = nullptr;
delete prevRop;
continue;
}
// throw exception unless disjoint or identical
bool disjoint = prevRop->lastIndex < rop->index || prevRop->index > rop->lastIndex;
// Delete special case of replace (text==null):
// D.i-j.u D.x-y.v | boundaries overlap combine to max(min)..max(right)
if (prevRop->text.empty() && rop->text.empty() && !disjoint) {
delete rewrites[prevRop->instructionIndex];
rewrites[prevRop->instructionIndex] = nullptr; // kill first delete
rop->index = std::min(prevRop->index, rop->index);
rop->lastIndex = std::max(prevRop->lastIndex, rop->lastIndex);
std::cout << "new rop " << rop << std::endl;
rewrites[prevRop->instructionIndex] = nullptr; // kill first delete
delete prevRop;
}
else if (!disjoint) {
throw IllegalArgumentException("replace op boundaries of " + rop->toString() +
Expand All @@ -379,8 +382,8 @@ std::unordered_map<size_t, TokenStreamRewriter::RewriteOperation*> TokenStreamRe
// whole token buffer so no lazy eval issue with any templates
iop->text = catOpText(&iop->text, &prevIop->text);
// delete redundant prior insert
delete rewrites[prevIop->instructionIndex];
rewrites[prevIop->instructionIndex] = nullptr;
delete prevIop;
}
}
// look for replaces where iop.index is in range; error
Expand Down
2 changes: 1 addition & 1 deletion runtime/Cpp/runtime/src/atn/ATNDeserializationOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ATNDeserializationOptions::ATNDeserializationOptions(ATNDeserializationOptions *
_generateRuleBypassTransitions(options->_generateRuleBypassTransitions) {}

const ATNDeserializationOptions& ATNDeserializationOptions::getDefaultOptions() {
static const ATNDeserializationOptions* const defaultOptions = new ATNDeserializationOptions();
static const std::unique_ptr<const ATNDeserializationOptions> defaultOptions = std::make_unique<const ATNDeserializationOptions>();
return *defaultOptions;
}

Expand Down
2 changes: 0 additions & 2 deletions runtime/Go/antlr/go.sum

This file was deleted.

8 changes: 3 additions & 5 deletions runtime/Go/antlr/v4/atn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package antlr

import "sync"

// ATNInvalidAltNumber is used to represent an ALT number that has yet to be calculated or
// which is invalid for a particular struct such as [*antlr.BaseRuleContext]
var ATNInvalidAltNumber int
Expand Down Expand Up @@ -56,9 +54,9 @@ type ATN struct {
//
states []ATNState

mu sync.Mutex
stateMu sync.RWMutex
edgeMu sync.RWMutex
mu Mutex
stateMu RWMutex
edgeMu RWMutex
}

// NewATN returns a new ATN struct representing the given grammarType and is used
Expand Down
3 changes: 0 additions & 3 deletions runtime/Go/antlr/v4/atn_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ func NewATNConfig1(c *ATNConfig, state ATNState, context *PredictionContext) *AT
// NewATNConfig creates a new ATNConfig instance given an existing config, a state, a context and a semantic context, other 'constructors'
// are just wrappers around this one.
func NewATNConfig(c *ATNConfig, state ATNState, context *PredictionContext, semanticContext SemanticContext) *ATNConfig {
if semanticContext == nil {
panic("semanticContext cannot be nil") // TODO: Remove this - probably put here for some bug that is now fixed
}
b := &ATNConfig{}
b.InitATNConfig(c, state, c.GetAlt(), context, semanticContext)
b.cType = parserConfig
Expand Down
5 changes: 2 additions & 3 deletions runtime/Go/antlr/v4/jcollect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"container/list"
"runtime/debug"
"sort"
"sync"
)

// Collectable is an interface that a struct should implement if it is to be
Expand Down Expand Up @@ -587,12 +586,12 @@ type VisitRecord struct {

type VisitList struct {
cache *list.List
lock sync.RWMutex
lock RWMutex
}

var visitListPool = VisitList{
cache: list.New(),
lock: sync.RWMutex{},
lock: RWMutex{},
}

// NewVisitRecord returns a new VisitRecord instance from the pool if available.
Expand Down
1 change: 1 addition & 0 deletions runtime/Go/antlr/v4/ll1_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (la *LL1Analyzer) getDecisionLookahead(s ATNState) []*IntervalSet {
for alt := 0; alt < count; alt++ {

look[alt] = NewIntervalSet()
// TODO: This is one of the reasons that ATNConfigs are allocated and freed all the time - fix this tomorrow jim!
lookBusy := NewJStore[*ATNConfig, Comparator[*ATNConfig]](aConfEqInst, ClosureBusyCollection, "LL1Analyzer.getDecisionLookahead for lookBusy")
la.look1(s.GetTransitions()[alt].getTarget(), nil, BasePredictionContextEMPTY, look[alt], lookBusy, NewBitSet(), false, false)

Expand Down
41 changes: 41 additions & 0 deletions runtime/Go/antlr/v4/mutex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build !antlr.nomutex
// +build !antlr.nomutex

package antlr

import "sync"

// Mutex is a simple mutex implementation which just delegates to sync.Mutex, it
// is used to provide a mutex implementation for the antlr package, which users
// can turn off with the build tag -tags antlr.nomutex
type Mutex struct {
mu sync.Mutex
}

func (m *Mutex) Lock() {
m.mu.Lock()
}

func (m *Mutex) Unlock() {
m.mu.Unlock()
}

type RWMutex struct {
mu sync.RWMutex
}

func (m *RWMutex) Lock() {
m.mu.Lock()
}

func (m *RWMutex) Unlock() {
m.mu.Unlock()
}

func (m *RWMutex) RLock() {
m.mu.RLock()
}

func (m *RWMutex) RUnlock() {
m.mu.RUnlock()
}
31 changes: 31 additions & 0 deletions runtime/Go/antlr/v4/mutex_nomutex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// +build antlr.nomutex

package antlr

type Mutex struct{}

func (m *Mutex) Lock() {
// No-op
}

func (m *Mutex) Unlock() {
// No-op
}

type RWMutex struct{}

func (m *RWMutex) Lock() {
// No-op
}

func (m *RWMutex) Unlock() {
// No-op
}

func (m *RWMutex) RLock() {
// No-op
}

func (m *RWMutex) RUnlock() {
// No-op
}
12 changes: 6 additions & 6 deletions runtime/Go/antlr/v4/prediction_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package antlr

import (
"fmt"
"golang.org/x/exp/slices"
"strconv"
)

Expand Down Expand Up @@ -115,6 +114,9 @@ func (p *PredictionContext) Hash() int {
}

func (p *PredictionContext) Equals(other Collectable[*PredictionContext]) bool {
if p == other {
return true
}
switch p.pcType {
case PredictionContextEmpty:
otherP := other.(*PredictionContext)
Expand All @@ -141,18 +143,16 @@ func (p *PredictionContext) ArrayEquals(o Collectable[*PredictionContext]) bool

// Must compare the actual array elements and not just the array address
//
return slices.Equal(p.returnStates, other.returnStates) &&
slices.EqualFunc(p.parents, other.parents, func(x, y *PredictionContext) bool {
return x.Equals(y)
})
return intSlicesEqual(p.returnStates, other.returnStates) &&
pcSliceEqual(p.parents, other.parents)
}

func (p *PredictionContext) SingletonEquals(other Collectable[*PredictionContext]) bool {
if other == nil {
return false
}
otherP := other.(*PredictionContext)
if otherP == nil {
if otherP == nil || otherP.pcType != PredictionContextSingleton {
return false
}

Expand Down
3 changes: 1 addition & 2 deletions runtime/Go/antlr/v4/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"sort"
"strconv"
"sync"
)

// This file allows the user to collect statistics about the runtime of the ANTLR runtime. It is not enabled by default
Expand All @@ -30,7 +29,7 @@ type goRunStats struct {
// within this package.
//
jStats []*JStatRec
jStatsLock sync.RWMutex
jStatsLock RWMutex
topN int
topNByMax []*JStatRec
topNByUsed []*JStatRec
Expand Down
Loading

0 comments on commit b77cb52

Please sign in to comment.