Skip to content

Commit

Permalink
1/3 Add Antlr dependency and write first draft of AeroShell grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabobko committed Jul 7, 2024
1 parent 3636df4 commit cd13c0a
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# Swift package manager
/.build
/.vscode
# External dependencies
/.deps

# XCode User settings
xcuserdata/
Expand Down
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
excluded:
- .build
- .xcode-build
- ./Sources/AeroShellParserGenerated

# https://realm.github.io/SwiftLint/rule-directory.html
only_rules:
Expand Down
4 changes: 3 additions & 1 deletion Brewfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
brew "asciidoctor"
brew "pygments"

brew "antlr"
brew "bash"
brew "fish"
brew "gsed"
brew "pygments"
brew "swiftlint"
brew "wget"
brew "xcbeautify"
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "antlr4",
"kind" : "remoteSourceControl",
"location" : "https:/antlr/antlr4",
"state" : {
"revision" : "7ed420ff2c78d62883875c442d75f32e73bc86c8",
"version" : "4.13.1"
}
},
{
"identity" : "bluesocket",
"kind" : "remoteSourceControl",
Expand Down
9 changes: 9 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let package = Package(
.package(url: "https:/LebJe/TOMLKit", exact: "0.5.5"),
.package(url: "https:/Quick/Nimble", exact: "12.0.0"),
.package(url: "https:/apple/swift-collections", exact: "1.1.0"),
.package(url: "https:/antlr/antlr4", from: "4.13.1")
],
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
Expand All @@ -28,10 +29,18 @@ let package = Package(
.product(name: "Collections", package: "swift-collections"),
]
),
.target(
name: "AeroShellParserGenerated",
dependencies: [
.product(name: "Antlr4Static", package: "antlr4"),
]
),
.target(
name: "AppBundle",
dependencies: [
.target(name: "Common"),
.target(name: "AeroShellParserGenerated"),
.product(name: "Antlr4Static", package: "antlr4"),
.product(name: "Socket", package: "BlueSocket"),
.product(name: "HotKey", package: "HotKey"),
.product(name: "TOMLKit", package: "TOMLKit"),
Expand Down
3 changes: 3 additions & 0 deletions build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ codesign -v .release/aerospace
############

mkdir -p .release/AeroSpace-v$build_version/manpage && cp .man/*.1 .release/AeroSpace-v$build_version/manpage
mkdir -p .release/AeroSpace-v$build_version/license &&
cp ./LICENSE .release/AeroSpace-v$build_version/license/AeroSpace-LICENSE &&
cp ./third-party-license/* .release/AeroSpace-v$build_version/license &&
cp -r .shell-completion .release/AeroSpace-v$build_version/shell-completion
cd .release
mkdir -p AeroSpace-v$build_version/bin && cp -r aerospace AeroSpace-v$build_version/bin
Expand Down
11 changes: 11 additions & 0 deletions generate-aero-shell-parser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
cd "$(dirname "$0")"
source ./script/setup.sh

test -e ./.deps/python-venv/bin/antlr4 || ./script/install-antlr.sh
source ./.deps/python-venv/bin/activate
antlr4 -no-listener -Dlanguage=Swift -o ./Sources/AeroShellParserGenerated ./grammar/AeroShellLexer.g4 ./grammar/AeroShellParser.g4

# Antlr generates weird *.interp and *.tokens files
rm ./Sources/AeroShellParserGenerated/grammar/*.interp
rm ./Sources/AeroShellParserGenerated/grammar/*.tokens
10 changes: 10 additions & 0 deletions generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source ./script/setup.sh

export XCODEGEN_AEROSPACE_CODE_SIGN_IDENTITY="aerospace-codesign-certificate"
build_version="0.0.0-SNAPSHOT"
all=0
while [[ $# -gt 0 ]]; do
case $1 in
--build-version)
Expand All @@ -16,13 +17,22 @@ while [[ $# -gt 0 ]]; do
shift
shift
;;
--all)
all=1
shift
;;
*)
echo "Unknown option $1"
exit 1
;;
esac
done

if [ $all -eq 1 ]; then
# Grammar is not expected to change very often, that's why it's not regenerated by default
./generate-aero-shell-parser.sh
fi

cat > Sources/Common/versionGenerated.swift <<EOF
// FILE IS GENERATED BY generate.sh
public let aeroSpaceAppVersion = "$build_version"
Expand Down
31 changes: 31 additions & 0 deletions grammar/AeroShellLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// AeroShell lexer grammar. Powered by https:/antlr/antlr4
// Use ./generate-aero-shell-parser.sh to regenerate grammar code
lexer grammar AeroShellLexer;

RESERVED : '"""' | '\'\'\'' ;

SINGLE_QUOTED_STRING : '\'' SINGLE_QUOTED_STRING_CONTENT '\'' ;
fragment SINGLE_QUOTED_STRING_CONTENT : .*? ;

LDQUOTE : '"' -> pushMode(IN_DSTRING) ;
LPAR : '(' -> pushMode(DEFAULT_MODE) ;
INTERPOLATION_START : '$(' -> pushMode(DEFAULT_MODE) ;
RPAR : ')' -> popMode ;

WORD : ([a-zA-Z0-9] | '.' | '_' | '-' | '+' | '/' | '%' | '{' | '}' | '@' | ',' | '[' | ']' | '=' | '^')+ ;
AND : '&&' ;
PIPE : '|' ;
OR : '||' ;
NOT : '!' SPACES ; // require space to make it possible use bang in arguments (not used yet, and maybe it's worth to allow '!false')
SEMICOLON : ';' ;
NEWLINES : '\n' (SPACES | '\n')* ;

ESCAPE_NEWLINE : '\\' SPACES? COMMENT? '\n' -> skip ;
COMMENT : '#' ~('\n')* -> skip ;
SPACES : [ \t]+ -> skip ;

mode IN_DSTRING;
TEXT : ~('\\' | '"' | '$')+ ;
INTERPOLATION_START_IN_DSTRING : '$(' -> pushMode(DEFAULT_MODE) ;
ESCAPE_SEQUENCE : '\\' . ;
RDQUOTE : '"' -> popMode ;
39 changes: 39 additions & 0 deletions grammar/AeroShellParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// AeroShell parser grammar. Powered by https:/antlr/antlr4
// Use ./generate-aero-shell-parser.sh to regenerate grammar code
parser grammar AeroShellParser;

options {
tokenVocab = './grammar/AeroShellLexer';
}

root : program EOF | EOF ; // Consume ALL the input

program
: NOT program #Not

| program NEWLINES PIPE program #Pipe
| program PIPE program #Pipe

| program NEWLINES AND program #And
| program AND program #And

| program NEWLINES OR program #Or
| program OR program #Or

| program (SEMICOLON | NEWLINES) (program)*? #Seq
| LPAR program RPAR #Parens
| arg+ #Args
;

arg
: WORD #Word
| LDQUOTE dStringFragment* RDQUOTE #DQuotedString
| INTERPOLATION_START program RPAR #Substitution
| SINGLE_QUOTED_STRING #SQuotedString
;

dStringFragment
: TEXT
| ESCAPE_SEQUENCE
| INTERPOLATION_START_IN_DSTRING program RPAR
;
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ swift test
./run-cli.sh -v
swiftlint lint --quiet

./generate.sh
./generate.sh --all
./script/check-uncommitted-files.sh
7 changes: 7 additions & 0 deletions script/install-antlr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
cd "$(dirname "$0")/.."
source ./script/setup.sh

python3 -m venv .deps/python-venv
source .deps/python-venv/bin/activate
python3 -m pip install 'antlr4-tools==0.2.1'
28 changes: 28 additions & 0 deletions third-party-license/antlr-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

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 name of copyright holders 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 REGENTS 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.

0 comments on commit cd13c0a

Please sign in to comment.