Browse Source

Improvements to run command, starting on Grammar

main
Dustin Wilson 3 years ago
parent
commit
e27408c662
  1. 136
      data/lua/tree-sitter-lua.json
  2. 16
      lib/Grammar.php
  3. 12
      lib/Grammar/Registry.php
  4. 68
      run

136
data/lua/tree-sitter-lua.json

@ -1,136 +0,0 @@
{
"name": "Lua",
"scopeName": "source.lua",
"type": "tree-sitter",
"parser": "tree-sitter-lua",
"fileTypes": [ "lua" ],
"folds": [
{
"type": [
"do_statement",
"while_statement",
"repeat_statement",
"for_statement",
"for_in_statement",
"function",
"local_function",
"function_definition",
"table"
],
"end": { "index": -1 }
},
{
"type": ["if_statement","elseif"],
"start": { "index": 1 },
"end": { "type": [ "else", "elseif" ] }
},
{
"type": "if_statement",
"start": { "index": 1 },
"end": { "index": -1 }
},
{
"type": "elseif",
"start": { "index": 1 }
},
{
"type": "else",
"start": { "index": 0 }
}
],
"comments": {
"start": "-- "
},
"scopes": {
"program": "source.lua",
"comment": "comment",
"string": "string",
"number": "constant.numeric",
"function > function_name > identifier": "entity.name.function",
"function > function_name > property_identifier": "entity.name.function",
"function > function_name > method": "entity.name.function",
"function > parameters > identifier": "variable.parameter",
"local_function > identifier": "entity.name.function",
"local_function > parameters > identifier": "variable.parameter",
"function_call > identifier": "entity.name.function",
"function_call > property_identifier": "entity.name.function",
"function_call > method": "entity.name.function",
"function_definition > parameters > identifier": "variable.parameter",
"self": "variable.language",
"next": "entity.name.function",
"spread": "variable.parameter",
"global_variable": "variable.language",
"nil": "constant.language.nil",
"true": "constant.language.boolean",
"false": "constant.language.boolean",
"'local'": "storage.modifier",
"'function'": "storage.type.function",
"label_statement": "storage.type.label",
"'{'": "punctuation.definition.table.begin",
"'}'": "punctuation.definition.table.end",
"'('": "punctuation.definition.parameters.begin",
"')'": "punctuation.definition.parameters.end",
"'do'": "keyword.control",
"'return'": "keyword.control",
"'if'": "keyword.control",
"'then'": "keyword.control",
"'elseif'": "keyword.control",
"'else'": "keyword.control",
"'while'": "keyword.control",
"'repeat'": "keyword.control",
"'until'": "keyword.control",
"'for'": "keyword.control",
"'in'": "keyword.control",
"'goto'": "keyword.control",
"'end'": "keyword.control",
"'or'": "keyword.operator.logical",
"'and'": "keyword.operator.logical",
"'not'": "keyword.operator.logical",
"'|'": "keyword.operator.logical",
"'&'": "keyword.operator.logical",
"'~'": "keyword.operator.logical",
"'='": "keyword.operator.assignment",
"'<'": "keyword.operator.comparison",
"'<='": "keyword.operator.comparison",
"'=='": "keyword.operator.comparison",
"'~='": "keyword.operator.comparison",
"'>='": "keyword.operator.comparison",
"'>'": "keyword.operator.comparison",
"'<<'": "keyword.operator.arithmetic.bitwise",
"'>>'": "keyword.operator.arithmetic.bitwise",
"'+'": "keyword.operator.arithmetic",
"'-'": "keyword.operator.arithmetic",
"'*'": "keyword.operator.arithmetic",
"'/'": "keyword.operator.arithmetic",
"'//'": "keyword.operator.arithmetic",
"'%'": "keyword.operator.arithmetic",
"'..'": "keyword.operator.arithmetic",
"'^'": "keyword.operator.arithmetic",
"'#'": "keyword.operator.arithmetic",
"'.'": "punctuation.separator.object",
"','": "punctuation.separator.object",
"':'": "punctuation.separator.method",
"';'": "punctuation.separator.statement"
}
}

16
lib/Grammar.php

@ -0,0 +1,16 @@
<?php
/** @license MIT
* Copyright 2021 Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace dW\Lit;
interface Grammar {
public static array $fileTypes;
public static string $firstLineMatch;
public static string $name;
public static GrammarPattern $patterns;
public static GrammarRepository $repository;
public static string $scopeName;
}

12
lib/Grammar/Registry.php

@ -34,18 +34,18 @@ class Registry {
return false;
}
public static function set(string $grammarPath, bool $force = false): bool {
if (!file_exists($grammarPath)) {
throw new \Exception("Path \"$grammarPath\" either does not exist or you do not have permission to view the file.");
public static function import(string $jsonPath, bool $force = false): bool {
if (!file_exists($jsonPath)) {
throw new \Exception("Path \"$jsonPath\" either does not exist or you do not have permission to view the file.");
}
$grammar = json_decode(file_get_contents($grammarPath), true);
$grammar = json_decode(file_get_contents($jsonPath), true);
if ($grammar === null) {
throw new \Exception("\"$grammarPath\" is not a valid grammar file.");
throw new \Exception("\"$jsonPath\" is not a valid grammar JSON file.");
}
if (!isset($grammar['scopeName'])) {
throw new \Exception("\"$grammarPath\" is missing the required scopeName property.");
throw new \Exception("\"$jsonPath\" is missing the required scopeName property.");
}
if (!$force && isset(self::$grammars[$grammar['scopeName']])) {

68
run

@ -19,30 +19,30 @@
##? * bash >=5.0
##? * composer >=2.0.6
##? * docopts >=0.6.3
##? * gnu awk >=5.1.0
##? * gnu coreutils >=8.0
##? * gnu parallel >=20180922
##? * GNU awk >=5.1.0
##? * GNU coreutils >=8.0
##? * GNU parallel >=20180922
##? * yarn >=1.22.10
##?
##? Arch Linux:
##? ```
##? pacman -S composer docopts parallel yarn
##? ```
##? ```
##? pacman -S composer docopts parallel yarn
##? ```
##?
##? macOS:
##? ```
##? brew install bash composer coreutils docopts gawk parallel yarn
##? echo \"/usr/local/bin/bash\" >> /etc/shells
##? chsh -s /usr/local/bin/bash
##? ```
##? ```
##? brew install bash composer coreutils docopts gawk parallel yarn
##? echo \"/usr/local/bin/bash\" >> /etc/shells
##? chsh -s /usr/local/bin/bash
##? ```
##?
##? Building the grammars is then a matter of running the following commands
##? from the project folder:
##?
##? ```
##? ./run init
##? ./run build
##? ```
##? ```
##? ./run init
##? ./run build
##? ```
##?
##?
##? Commands:
@ -57,29 +57,41 @@
##? needed
##? List the language grammars that are included in the current grammars
##? but are not in the data/ folder.
##?
set -e
cwd=$(pwd)
PATH="$cwd/node_modules/.bin:$PATH"
help=$(grep "^##?" "$0" | cut -c 5-)
eval "$(docopts -h "$help" : "$@")"
eval "$(docopts -A args -h "$help" : "$@")"
fail() {
echo $help
exit 1
}
# Check for bash 4+
shopt -s globstar extglob || help
# Check for bash 4+ and set extra glob options
shopt -s globstar extglob || fail
# macOS uses the BSD version of awk which isn't compatible with the linux
# version, so this script requires Mac users to install the GNU version; it
# therefore has a different name on macOS.
awkcmd="awk"
if [ "$(uname)" == "Darwin" ]; then
awkcmd="gawk"
fi
# Check for presence of dependencies
if (( $(which $awkcmd composer parallel yarn | wc -l) < 4 )); then help; fi
if (( $(which $awkcmd composer parallel yarn | wc -l) < 4 )); then fail; fi
# Process the subcommand
subcmd="build"
subcmd=$([ "${args[init]}" == "true" ] && echo "init" || echo "$subcmd")
subcmd=$([ "${args[needed]}" == "true" ] && echo "needed" || echo "$subcmd")
read -r -d '' grammarList <<'EOF'
# Grammar table of the github repository location followed by its local folder
# name
read -r -d '' grammarTable <<'EOF'
franzheidl/atom-applescript applescript
atom/language-c c
atom/language-coffee-script coffeescript
@ -102,14 +114,14 @@ atom/language-typescript typescript
atom/language-xml xml
EOF
case $1 in
case $subcmd in
init)
yarn install
mkdir -p "$cwd"/deps
rm -rf "$cwd"/deps/*
cd "$cwd"/deps
printf "$grammarList" | parallel --colsep ' ' "git clone https://github.com/{1}.git"
printf "$grammarTable" | parallel --colsep ' ' "git clone https://github.com/{1}.git"
cd "$cwd"
;;
@ -122,14 +134,22 @@ needed)
build | *)
rm -rf "$cwd"/data/*
# Copy grammars folders from all the local language grammar repositories to a
# temporary folder with the names provided in the table above
temp=$(mktemp -d)
printf "$grammarList" | parallel --colsep ' ' "mkdir -p $temp/{2} &&
printf "$grammarTable" | parallel --colsep ' ' "mkdir -p $temp/{2} &&
cp -r \"$cwd\"/deps/\$(basename {1})/grammars/* $temp/{2}/"
# Convert all relevant CSON files to a format that can actually be used :)
ls -1 $temp/**/!(tree-sitter-*).cson | parallel "csonc --output={.}.json {} && rm {}"
rm -rf $temp/**/*.cson
# Remove any remaining tree sitter files; will also remove JSON ones from
# packages that didn't use CSON
rm $temp/**/tree-sitter-*
# Lastly, move everything over to the project's data folder and delete the
# temporary folder
mv $temp/* "$cwd"/data/
rmdir $temp
;;
esac
Loading…
Cancel
Save