Updated merge_map

This commit is contained in:
thomashii 2021-09-12 08:03:13 +08:00
parent 4c9bc1c9be
commit 9cb60b0606
14 changed files with 146 additions and 90 deletions

View file

@ -2,9 +2,13 @@
## About
This repository contains the common utility packages required for developing backend framework.
This repository contains the common utility packages required for developing dart backend framework.
## Available Packages
* html_builder
* Html Builder
* Code Buffer
* Combinator
* Merge Map
* Symbol Table

View file

@ -1,16 +1,29 @@
# 2.0.2
# Change Log
## 3.0.0
* Upgraded from `pendantic` to `lints` linter
* Published as `belatuk_merge_map` package
* Fixed linter warnings
## 2.0.2
* Resolve static analysis warnings
# 2.0.1
## 2.0.1
* Updated README
# 2.0.0
## 2.0.0
* Migrated to work with Dart SDK 2.12.x NNBD
# 1.0.2
## 1.0.2
* Add an example, for Pub's sake.
# 1.0.1
## 1.0.1
* Add a specific constraint on Dart versions, to prevent Pub from rejecting all packages that depend on
`merge_map` (the entire Angel framework).
* Add generic type support

View file

@ -1,21 +1,29 @@
MIT License (MIT)
BSD 3-Clause License
Copyright (c) 2021 dukefirehawk.com
Copyright (c) 2021, dukefirehawk.com
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -1,18 +1,20 @@
# angel3_merge_map
[![version](https://img.shields.io/badge/pub-v2.0.2-brightgreen)](https://pub.dartlang.org/packages/angel3_merge_map)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
# Belatuk Merge Map
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/merge_map/LICENSE)
[![version](https://img.shields.io/badge/pub-v3.0.1-brightgreen)](https://pub.dartlang.org/packages/belatuk_merge_map)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![License](https://img.shields.io/github/license/dart-backend/belatuk-common-utilities)](https://github.com/dart-backend/belatuk-common-utilities/packages/code_buffer/LICENSE)
**Replacement of `package:merge_map` with breaking changes to support NNBD.**
Combine multiple Maps into one. Equivalent to
[Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
in JS.
# Example
## Example
```dart
import "package:angel3_merge_map/angel3_merge_map.dart";
import "package:belatuk_merge_map/belatuk_merge_map.dart";
void main() {
Map map1 = {'hello': 'world'};
@ -22,4 +24,4 @@ void main() {
// {hello: world, foo: {bar: baz, john: doe, this: overrides previous maps}}
}
```
```

View file

@ -1,4 +1 @@
include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false
include: package:lints/recommended.yaml

View file

@ -1,4 +1,4 @@
import 'package:angel3_merge_map/angel3_merge_map.dart';
import 'package:belatuk_merge_map/belatuk_merge_map.dart';
void main() {
// ignore: omit_local_variable_types

View file

@ -2,15 +2,17 @@
library angel3_merge_map;
dynamic _copyValues<K, V>(
Map<K, V> from, Map<K, V?>? to, bool recursive, bool acceptNull) {
Map<K, V> from, Map<K, V?> to, bool recursive, bool acceptNull) {
for (var key in from.keys) {
if (from[key] is Map<K, V> && recursive) {
if (!(to![key] is Map<K, V>)) {
if (to[key] is! Map<K, V>) {
to[key] = <K, V>{} as V;
}
_copyValues(from[key] as Map, to[key] as Map?, recursive, acceptNull);
_copyValues(from[key] as Map, to[key] as Map, recursive, acceptNull);
} else {
if (from[key] != null || acceptNull) to![key] = from[key];
if (from[key] != null || acceptNull) {
to[key] = from[key];
}
}
}
}
@ -27,8 +29,8 @@ dynamic _copyValues<K, V>(
Map<K, V> mergeMap<K, V>(Iterable<Map<K, V>> maps,
{bool recursive = true, bool acceptNull = false}) {
var result = <K, V>{};
maps.forEach((Map<K, V> map) {
for (var map in maps) {
_copyValues(map, result, recursive, acceptNull);
});
}
return result;
}

View file

@ -1,9 +1,9 @@
name: angel3_merge_map
version: 2.0.2
name: belatuk_merge_map
version: 3.0.0
description: Combine multiple Maps into one. Equivalent to Object.assign in JS.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/merge_map
homepage: https://github.com/dart-backend/belatuk-common-utilities/tree/main/packages/merge_map
environment:
sdk: '>=2.12.0 <3.0.0'
dev_dependencies:
test: ^1.17.4
pedantic: ^1.11.0
lints: ^1.0.0

View file

@ -1,4 +1,4 @@
import 'package:angel3_merge_map/angel3_merge_map.dart';
import 'package:belatuk_merge_map/belatuk_merge_map.dart';
import 'package:test/test.dart';
void main() {

View file

@ -1,16 +1,28 @@
# 2.0.2
# Change Log
## 3.0.0
* Upgraded from `pendantic` to `lints` linter
* Published as `belatuk_symbol_table` package
## 2.0.2
* Resolved static analysis warnings
# 2.0.1
## 2.0.1
* Resolved static analysis warnings
# 2.0.0
## 2.0.0
* Migrated to work with Dart SDK 2.12.x NNBD
## 1.0.4
* Added `context` to `SymbolTable`.
## 1.0.3
* Converted `Visibility` into a `Comparable` class.
* Renamed `add` -> `create`, `put` -> `assign`, and `allVariablesOfVisibility` -> `allVariablesWithVisibility`.
* Added tests for `Visibility` comparing, and `depth`.
@ -18,9 +30,10 @@
* Fixed a typo in `remove` that would have prevented it from working correctly.
## 1.0.2
* Added `depth` to `SymbolTable`.
* Added `symbolTable` to `Variable`.
* Deprecated the redundant `Constant` class.
* Deprecated `Variable.markAsPrivate()`.
* Added the `Visibility` enumerator.
* Added the field `visibility` to `Variable`.
* Added the field `visibility` to `Variable`.

View file

@ -1,21 +1,29 @@
MIT License (MIT)
BSD 3-Clause License
Copyright (c) 2021 dukefirehawk.com
Copyright (c) 2021, dukefirehawk.com
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -1,5 +1,6 @@
# angel3_symbol_table
[![version](https://img.shields.io/badge/pub-v2.0.2-brightgreen)](https://pub.dartlang.org/packages/angel3_symbol_table)
# Belatuk Merge Map
[![version](https://img.shields.io/badge/pub-v3.0.0-brightgreen)](https://pub.dartlang.org/packages/belatuk_symbol_table)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
@ -9,7 +10,8 @@ A generic symbol table implementation in Dart, with support for scopes and const
The symbol tables produced by this package are hierarchical (in this case, tree-shaped),
and utilize basic memoization to speed up repeated lookups.
# Variables
## Variables
To represent a symbol, use `Variable`. I opted for the name
`Variable` to avoid conflict with the Dart primitive `Symbol`.
@ -28,6 +30,7 @@ foo.value = 'baz'; // Also throws a StateError - Once a variable is locked, it c
```
## Visibility
Variables are *public* by default, but can also be marked as *private* or *protected*. This can be helpful if you are trying
to determine which symbols should be exported from a library or class.
@ -36,7 +39,8 @@ myVariable.visibility = Visibility.protected;
myVariable.visibility = Visibility.private;
```
# Symbol Tables
## Symbol Tables
It's easy to create a basic symbol table:
```dart
@ -66,7 +70,8 @@ var symbol = doubles.resolveOrCreate('one', value: 1.0);
var symbol = doubles.resolveOrCreate('one', value: 1.0, constant: true);
```
# Exporting Symbols
## Exporting Symbols
Due to the tree structure of symbol tables, it is extremely easy to
extract a linear list of distinct variables, with variables lower in the hierarchy superseding their parents
(effectively accomplishing variable shadowing).
@ -83,15 +88,17 @@ var exportedSymbols = mySymbolTable.allPublicVariables;
```
It's easy to extract symbols of a given visibility:
```dart
var exportedSymbols = mySymbolTable.allVariablesWithVisibility(Visibility.protected);
```
# Child Scopes
## Child Scopes
There are three ways to create a new symbol table:
### Regular Children
## Regular Children
This is what most interpreters need; it simply creates a symbol table with the current symbol table
as its parent. The new scope can define its own symbols, which will only shadow the ancestors within the
correct scope.
@ -101,18 +108,21 @@ var child = mySymbolTable.createChild();
var child = mySymbolTable.createChild(values: {...});
```
### Depth
#### Depth
Every symbol table has an associated `depth` attached to it, with the `depth` at the root
being `0`. When `createChild` is called, the resulting child has an incremented `depth`.
## Clones
### Clones
This creates a scope at the same level as the current one, with all the same variables.
```dart
var clone = mySymbolTable.clone();
```
## Forked Scopes
### Forked Scopes
If you are implementing a language with closure functions, you might consider looking into this.
A forked scope is a scope identical to the current one, but instead of merely copying references
to variables, the values of variables are copied into new ones.
@ -128,7 +138,8 @@ var forked = mySymbolTable.fork();
var forked = mySymbolTable.fork(values: {...});
```
# Creating Names
## Creating Names
In languages with block scope, oftentimes, identifiers will collide within a global scope.
To avoid this, symbol tables expose a `uniqueName()` method that simply attaches a numerical suffix to
an input name. The name is guaranteed to never be repeated within a specific scope.
@ -139,7 +150,8 @@ var name1 = mySymbolTable.uniqueName('foo'); // foo1
var name2 = mySymbolTable.uniqueName('foo'); // foo2
```
# `this` Context
## `this` Context
Many languages handle a sort of `this` context that values within a scope may
optionally be resolved against. Symbol tables can easily set their context
as follows:
@ -158,4 +170,4 @@ void bar() {
mySymbolTable.context = thisContext;
expect(mySymbolTable.createChild().createChild().context, thisContext);
}
```
```

View file

@ -1,4 +1 @@
include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false
include: package:lints/recommended.yaml

View file

@ -1,11 +1,11 @@
name: angel3_symbol_table
version: 2.0.2
name: belatuk_symbol_table
version: 3.0.0
description: A generic symbol table implementation in Dart, with support for scopes and constants.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/symbol_table
homepage: https://github.com/dart-backend/belatuk-common-utilities/tree/main/packages/symbol_table
environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
collection: ^1.15.0
dev_dependencies:
test: ^1.17.4
pedantic: ^1.11.0
lints: ^1.0.0