Skip to content

feat(experimental:template-language): start an own template language #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions .github/workflows/build-v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: Build v2
on:
push:
branches: [ '*' ]
paths-ignore:
- "**/docs/**"
- "**.md"
pull_request:
branches: [ main ]
workflow_call:

jobs:
check:
name: build crate
strategy:
fail-fast: false
matrix:
version: [ 'macos-latest', 'ubuntu-latest', 'windows-latest']
rust: [ nightly, stable ]
runs-on: ${{ matrix.version }}
steps:
- uses: actions/checkout@v2
- name: setup | rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
profile: minimal
components: clippy, rustfmt
- uses: Swatinem/rust-cache@v1
- run: cargo install cargo-insta
- run: cargo check
continue-on-error: ${{ matrix.rust == 'nightly' }}
- run: cargo fmt --all -- --check
continue-on-error: ${{ matrix.rust == 'nightly' }}
- run: cargo clippy --all-targets --all-features -- -D warnings
continue-on-error: ${{ matrix.rust == 'nightly' }}
- run: cargo test --all --locked -- -Z unstable-options
continue-on-error: ${{ matrix.rust == 'nightly' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: cargo insta test
continue-on-error: ${{ matrix.rust == 'nightly' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: smoke tests
run: |
cargo run -- --version
cargo run -- --help

audit:
name: security audit
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: setup | rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- name: audit
uses: actions-rs/audit-check@v1
continue-on-error: true
with:
token: ${{ secrets.GITHUB_TOKEN }}

publish-dry-run:
name: publish dry run
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: setup | rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- run: cargo publish --dry-run -p curlz

docs:
name: docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: setup | rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
default: true
profile: minimal
- uses: Swatinem/rust-cache@v1
- name: check documentation
env:
RUSTDOCFLAGS: -D warnings
run: cargo doc --no-deps --all-features
91 changes: 91 additions & 0 deletions curlz/src/curlz/templ-lang/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use crate::language::tokens::Span;

// using toml::Value here only because of lazyness
pub use toml::Value;

#[derive(Debug)]
pub struct Spanned<T> {
pub node: Box<T>,
pub span: Span,
}

impl<T> Spanned<T> {
pub fn new(node: T, span: Span) -> Self {

Check warning on line 13 in curlz/src/curlz/templ-lang/ast.rs

View check run for this annotation

Codecov / codecov/patch

curlz/src/curlz/templ-lang/ast.rs#L13

Added line #L13 was not covered by tests
Self {
node: Box::new(node),

Check warning on line 15 in curlz/src/curlz/templ-lang/ast.rs

View check run for this annotation

Codecov / codecov/patch

curlz/src/curlz/templ-lang/ast.rs#L15

Added line #L15 was not covered by tests
span,
}
}
}

#[derive(Debug)]
pub struct Template<'a> {
pub children: Vec<Stmt<'a>>,
}

#[derive(Debug)]
pub struct Var<'a> {
pub id: &'a str,
}

#[derive(Debug)]
pub struct SysVar<'a> {
pub id: &'a str,
}

#[derive(Debug)]
pub struct EmitRaw<'a> {
pub raw: &'a str,
}

#[derive(Debug)]
pub struct EmitExpr<'a> {
pub expr: Expr<'a>,
}

#[derive(Debug)]
pub struct Const {
pub value: Value,
}

#[derive(Debug)]
pub struct Call<'a> {
pub expr: Expr<'a>,
pub args: Vec<Expr<'a>>,
}

#[derive(Debug)]
pub enum Expr<'a> {
SysVar(Spanned<SysVar<'a>>),
Var(Spanned<Var<'a>>),
Const(Spanned<Const>),
Call(Spanned<Call<'a>>),
}

#[derive(Debug)]
pub enum Stmt<'a> {
Template(Spanned<Template<'a>>),
EmitRaw(Spanned<EmitRaw<'a>>),
EmitExpr(Spanned<EmitExpr<'a>>),
}

#[cfg(test)]
pub trait IntoSpanned {
fn spanned(self) -> Spanned<Self>
where
Self: Sized,
{
Spanned::new(
self,
Span {
start_line: 1,
start_col: 0,
end_line: 1,
end_col: 1,

Check warning on line 84 in curlz/src/curlz/templ-lang/ast.rs

View check run for this annotation

Codecov / codecov/patch

curlz/src/curlz/templ-lang/ast.rs#L78-L84

Added lines #L78 - L84 were not covered by tests
},
)
}
}

#[cfg(test)]
impl<T> IntoSpanned for T {}
33 changes: 33 additions & 0 deletions curlz/src/curlz/templ-lang/ast_visitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*!
this module contains AST related tooling such as the visitor trait and
the double dispatch (impl of [`AstVisitAcceptor`]) for all AST nodes.
*/
use crate::language::ast;

pub trait AstVisitAcceptor<'ast> {
fn accept<V: AstVisit<'ast>>(&self, visitor: &mut V);
}

pub trait AstVisit<'ast> {
fn visit_stmt(&mut self, _stmt: &ast::Stmt<'ast>) {}
fn visit_expr(&mut self, _expr: &ast::Expr<'ast>) {}
fn visit_emit_raw(&mut self, _raw: &ast::EmitRaw<'ast>) {}
}

impl<'ast> AstVisitAcceptor<'ast> for ast::Stmt<'ast> {
fn accept<V: AstVisit<'ast>>(&self, visitor: &mut V) {
visitor.visit_stmt(self);

Check warning on line 19 in curlz/src/curlz/templ-lang/ast_visitor.rs

View check run for this annotation

Codecov / codecov/patch

curlz/src/curlz/templ-lang/ast_visitor.rs#L18-L19

Added lines #L18 - L19 were not covered by tests
}
}

impl<'ast> AstVisitAcceptor<'ast> for ast::Expr<'ast> {
fn accept<V: AstVisit<'ast>>(&self, visitor: &mut V) {
visitor.visit_expr(self);

Check warning on line 25 in curlz/src/curlz/templ-lang/ast_visitor.rs

View check run for this annotation

Codecov / codecov/patch

curlz/src/curlz/templ-lang/ast_visitor.rs#L24-L25

Added lines #L24 - L25 were not covered by tests
}
}

impl<'ast> AstVisitAcceptor<'ast> for ast::EmitRaw<'ast> {
fn accept<V: AstVisit<'ast>>(&self, visitor: &mut V) {
visitor.visit_emit_raw(self);

Check warning on line 31 in curlz/src/curlz/templ-lang/ast_visitor.rs

View check run for this annotation

Codecov / codecov/patch

curlz/src/curlz/templ-lang/ast_visitor.rs#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}
Loading