Skip to content

Commit

Permalink
Parser rewrite (#20)
Browse files Browse the repository at this point in the history
* rewrite start and lexer

* make ranges f64 only

* "pushing what I have"

Co-authored-by: Sasial <[email protected]>

* fixed enum

* ty grammar parsed

Co-authored-by: Sasial <[email protected]>

* syntax parser should be done

* syntax tree and parser

* major changes to parser, irgen, output

Co-authored-by: Sasial <[email protected]>

* Update TS output to use new config file

* Update zap/src/config.rs

Co-authored-by: Sasial <[email protected]>

* Update cli/Cargo.toml

Co-authored-by: Sasial <[email protected]>

* remove logos and fix order

* remove todo

* move imports to top of file

* remove shortened name

* extract code out of macro

* update lib returns

* fix numlit regex

* Add diagnostics support to WASM/Playground

* chore: fmt

* fix incorrect tydecl grammar

* condense filter and map to filter_map

* add debugging config

* fix `struct` keyword being required in tagged enum structs

* lots of changes

Co-authored-by: Sasial <[email protected]>

* move and update ebnf grammar

* fix colorchoice to work in colorless terminals

* refactor error code constants in reports.rs

* add vscode to gitignore

* change casing opt

* big changes

* rename EmptyTab to EmptyTable

---------

Co-authored-by: Sasial <[email protected]>
  • Loading branch information
jackdotink and sasial-dev authored Dec 27, 2023
1 parent 107bba2 commit be79647
Show file tree
Hide file tree
Showing 30 changed files with 2,661 additions and 1,497 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/target
target

# Zap Input & Output
/network
network
net.zap

# Docs
Expand All @@ -14,3 +14,6 @@ zap/package

# Mac Moment
.DS_Store

# Editor Stuff
.vscode/
4 changes: 0 additions & 4 deletions .vscode/settings.json

This file was deleted.

37 changes: 36 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
clap = { version = "4.4.11", features = ["derive"] }
codespan-reporting = "0.11.0"
zap = { path = "../zap" }

[[bin]]
Expand Down
56 changes: 43 additions & 13 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ use std::path::PathBuf;

use anyhow::Result;
use clap::Parser;
use codespan_reporting::{
files::SimpleFile,
term::{
self,
termcolor::{ColorChoice, StandardStream},
},
};
use zap::run;

#[derive(Parser, Debug)]
Expand All @@ -16,26 +23,49 @@ fn main() -> Result<()> {

let config_path = args.config.unwrap();

let config = std::fs::read_to_string(config_path)?;
let config = std::fs::read_to_string(&config_path)?;

let code = run(config.as_str())?;
let ret = run(config.as_str());

if let Some(definitions) = code.server.definitions {
let mut path = code.server.path.clone();
path.set_extension("d.ts");
let code = ret.code;
let diagnostics = ret.diagnostics;

std::fs::write(path, definitions)?
}
if let Some(code) = code {
let server_path = code.server.path.unwrap_or(PathBuf::from("network/server.lua"));
let client_path = code.client.path.unwrap_or(PathBuf::from("network/client.lua"));

if let Some(parent) = server_path.parent() {
std::fs::create_dir_all(parent)?;
}

if let Some(parent) = client_path.parent() {
std::fs::create_dir_all(parent)?;
}

if let Some(definitions) = code.client.definitions {
let mut path = code.client.path.clone();
path.set_extension("d.ts");
std::fs::write(server_path.clone(), code.server.code)?;
std::fs::write(client_path.clone(), code.client.code)?;

std::fs::write(path, definitions)?
if let Some(defs) = code.server.defs {
std::fs::write(server_path.with_extension("d.ts"), defs)?;
}

if let Some(defs) = code.client.defs {
std::fs::write(client_path.with_extension("d.ts"), defs)?;
}
}

std::fs::write(code.server.path, code.server.contents)?;
std::fs::write(code.client.path, code.client.contents)?;
if diagnostics.is_empty() {
return Ok(());
}

let file = SimpleFile::new(config_path.to_str().unwrap(), config);

let writer = StandardStream::stderr(ColorChoice::Auto);
let config_term = codespan_reporting::term::Config::default();

for diagnostic in diagnostics {
term::emit(&mut writer.lock(), &config_term, &file, &diagnostic)?;
}

Ok(())
}
75 changes: 75 additions & 0 deletions config.ebnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
(* This file is exists only for the purpose of documentation *)
(* and reference. It is not used anywhere within zap. *)
(* Zap Rules *)
(* this section has whitespace *)
zap = {opt}, {evdecl | tydecl};
opt = "opt", (
"write_checks", ':', ("true" | "false")
| "server_output", ':', string
| "client_output", ':', string
| "typescript", ':', ("true" | "false")
), [';'];
evdecl = "event", ident, '=', '{',
"from", ':', ("Server" | "Client"), ',',
"type", ':', ("Reliable" | "Unreliable"), ',',
"call", ':', ("SingleSync" | "SingleAsync" | "ManySync" | "ManyAsync"), ',',
"data", ':', ty, [','],
'}', [';'];
tydecl = "type", ident, '=', ty, [';'];
ty = ty-num | ty-str | ty-arr | ty-map | ty-opt | ty-ref | ty-enum | ty-struct | ty-instance;
ty-num = ("f32" | "f64"), ['(', range-num,')']
| ("u8" | "u16" | "u32" | "i8" | "i16" | "i32"), ['(', range-int,')'];
ty-str = "string", ['(', range-int,')'];
ty-arr = ty, '[', range-num, ']';
ty-map = "map", '[', ty, ']', ':', ty;
ty-opt = ty, '?';
ty-ref = ident;
ty-struct = "struct", struct;
ty-enum = ty-enum-unit | ty-enum-tagged;
ty-enum-unit = "enum", '{', ident, {',', ident}, [','], '}';
ty-enum-tagged = "enum", string, '{', ident, struct, {',', ident, ty-struct}, [','], '}';
ty-instance = "Instance", ['(', ident, ')'];
struct-field = ident, ':', ty, [',', struct-field];
struct = '{', struct-field, [","], '}';
range-num = ""
| num, "..", num
| num, ".."
| "..", num
| ".."
| num;
range-int = ""
| int, "..", int
| int, ".."
| "..", int
| ".."
| int;
(* Base Rules *)
(* this section has no whitespace *)
ident = alpha, {alphanum | '_'};
string = '"', {alphanum}, '"';
num = int, ['.', int];
int = digit, {digit};
alphanum = alpha | digit;
alpha = 'a'..'z' | 'A'..'Z';
digit = '0'..'9';
43 changes: 38 additions & 5 deletions docs/.vitepress/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,20 @@ const beforeMount = (monaco: Monaco) => {
{ open: "{", close: "}" },
{ open: "[", close: "]" },
{ open: "(", close: ")" },
{ open: '"', close: '"' },
],
surroundingPairs: [
{ open: "{", close: "}" },
{ open: "[", close: "]" },
{ open: "(", close: ")" },
{ open: '"', close: '"' },
],
});
const Keywords = ["event", "opt", "type"] as const;
const TypeKeywords = ["enum", "struct", "map"] as const;
const Operators = ["true", "false"] as const;
const Locations = ["Server", "Client"] as const;
Expand All @@ -102,7 +106,7 @@ const beforeMount = (monaco: Monaco) => {
const Calls = ["SingleSync", "SingleAsync", "ManySync", "ManyAsync"] as const;
const Options = ["typescript", "write_checks", "casing", "output_server", "output_client"] as const;
const Options = ["typescript", "write_checks", "casing", "server_output", "client_output"] as const;
const Casing = ["PascalCase", "camelCase", "snake_case"] as const;
Expand All @@ -119,10 +123,10 @@ const beforeMount = (monaco: Monaco) => {
"i64",
"f32",
"f64",
"bool",
"boolean",
"string",
"Instance",
"Vector3"
"Vector3",
] as const;
const EventParamToArray = {
Expand All @@ -148,7 +152,7 @@ const beforeMount = (monaco: Monaco) => {
create: () => ({
defaultToken: "",
keywords: [...Keywords, ...Operators],
keywords: [...Keywords, ...TypeKeywords, ...Operators],
brackets: [
{ token: "delimiter.bracket", open: "{", close: "}" },
Expand Down Expand Up @@ -192,6 +196,9 @@ const beforeMount = (monaco: Monaco) => {
},
],
// "str" identifiers
[/\"\w+\"/, "regexp"],
// identifiers and keywords
[/(\w+):/, "identifier"],
[
Expand Down Expand Up @@ -312,6 +319,33 @@ const beforeMount = (monaco: Monaco) => {
range,
}));
if (wordBefore && !WordToArray[wordBefore.word]) {
identifiers.push(
{
label: "enum",
kind: monaco.languages.CompletionItemKind.Variable,
insertText: "enum ",
range: range,
},
{
label: "map",
kind: monaco.languages.CompletionItemKind.Snippet,
insertText: "map { [${1}]: ${2} }\n",
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: "Map",
range: range,
},
{
label: "struct",
kind: monaco.languages.CompletionItemKind.Snippet,
insertText: ["struct {", "\t${1}: ${2},", "}"].join("\n"),
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: "Struct",
range: range,
}
);
}
return { suggestions: identifiers };
}
},
Expand All @@ -325,4 +359,3 @@ const beforeMount = (monaco: Monaco) => {
width: 100%;
}
</style>

Loading

0 comments on commit be79647

Please sign in to comment.