Skip to content
Merged
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
116 changes: 81 additions & 35 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ tower-lsp = "0.20.0"
tower = { version = "0.4", features = ["util"] }
dashmap = "6.1.0"
ropey = "1.6.1"
tx3-lang = "0.11.0"
tx3-lang = "0.14.0"
tx3-tir = "0.14.0"
pest = "2.7.15"
miette = "7.5.0"
serde_json = "1.0.140"
Expand Down
6 changes: 3 additions & 3 deletions src/cmds/generate_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ pub async fn run(
) -> Result<Option<Value>, Error> {
let args: Args = args.try_into()?;

let protocol = context.get_document_protocol(&args.document_url)?;
let mut program = context.get_document_program(&args.document_url)?;

let ast = protocol.ast().to_owned();
tx3_lang::analyzing::analyze(&mut program).ok().unwrap();

let out = json!({
"ast": ast,
"ast": program,
});

Ok(Some(out))
Expand Down
11 changes: 6 additions & 5 deletions src/cmds/generate_diagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ pub async fn run(
) -> Result<Option<Value>, Error> {
let args: Args = args.try_into()?;

let protocol = context.get_document_protocol(&args.document_url)?;
let ast = protocol.ast().to_owned();
let mut program = context.get_document_program(&args.document_url)?;

let tx_svgs: Vec<Value> = ast
tx3_lang::analyzing::analyze(&mut program).ok().unwrap();

let tx_svgs: Vec<Value> = program
.txs
.iter()
.map(|tx| {
let svg = tx_to_svg(&ast, tx);
let svg = tx_to_svg(&program, tx);
json!({
"tx_name": tx.name,
"tx_name": tx.name.value,
"svg": svg
})
})
Expand Down
22 changes: 9 additions & 13 deletions src/cmds/generate_tir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::collections::HashMap;

use serde_json::{json, Value};

use tx3_tir::reduce::Apply;
use crate::{Context, Error};

#[derive(Debug)]
Expand Down Expand Up @@ -35,20 +33,18 @@ pub async fn run(
) -> Result<Option<Value>, Error> {
let args: Args = args.try_into()?;

let protocol = context.get_document_protocol(&args.document_url)?;
let mut program = context.get_document_program(&args.document_url)?;

tx3_lang::analyzing::analyze(&mut program).ok().unwrap();

let prototx = protocol.new_tx(&args.tx_name)?;
let tx = tx3_lang::lowering::lower(&program, &args.tx_name).unwrap();

let params = prototx
.find_params()
.iter()
.map(|(k, v)| (k.to_string(), serde_json::to_value(v).unwrap()))
.collect::<HashMap<String, Value>>();
let tir = tx3_tir::encoding::to_bytes(&tx);

let out = json!({
"tir": hex::encode(prototx.ir_bytes()),
"version": tx3_lang::ir::IR_VERSION,
"parameters": params,
"tir": hex::encode(&tir.0),
"version": tir.1,
"parameters": tx.params(),
});

Ok(Some(out))
Expand Down
16 changes: 6 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use thiserror::Error;
use tower_lsp::jsonrpc::ErrorCode;
use tower_lsp::lsp_types::*;
use tower_lsp::Client;
use tx3_lang::Protocol;

mod ast_to_svg;
mod cmds;
Expand All @@ -27,8 +26,8 @@ pub enum Error {
#[error("Document not found: {0}")]
DocumentNotFound(Url),

#[error("Protocol loading error: {0}")]
ProtocolLoadingError(#[from] tx3_lang::loading::Error),
#[error("Program parsing error: {0}")]
ProgramParsingError(#[from] tx3_lang::parsing::Error),

#[error("Tx3 Lowering error: {0}")]
TxLoweringError(#[from] tx3_lang::lowering::Error),
Expand All @@ -41,7 +40,7 @@ impl From<&Error> for ErrorCode {
Error::ParseError(_) => ErrorCode::InvalidParams,
Error::DocumentNotFound(_) => ErrorCode::InvalidParams,
Error::InvalidCommandArgs(_) => ErrorCode::InvalidParams,
Error::ProtocolLoadingError(_) => ErrorCode::InvalidRequest,
Error::ProgramParsingError(_) => ErrorCode::InvalidRequest,
Error::TxLoweringError(_) => ErrorCode::InvalidRequest,
}
}
Expand Down Expand Up @@ -243,7 +242,7 @@ impl Context {
token_modifiers: MOD_DECLARATION | MOD_DEFINITION,
});
}
visitor::SymbolAtOffset::TypeIdentifier(x) => {
visitor::SymbolAtOffset::TypeIdentifier(_x) => {
// TODO: wait for the introduction of `TypeAnnotation` in AST

// token_infos.push(TokenInfo {
Expand Down Expand Up @@ -315,12 +314,9 @@ impl Context {
Ok(document.value().clone())
}

fn get_document_protocol(&self, url_arg: &str) -> Result<Protocol, Error> {
fn get_document_program(&self, url_arg: &str) -> Result<tx3_lang::ast::Program, Error> {
let document = self.get_document(url_arg)?;

let protocol = Protocol::from_string(document.to_string()).load()?;

Ok(protocol)
tx3_lang::parsing::parse_string(document.to_string().as_str()).map_err(Error::ProgramParsingError)
}

async fn process_document(&self, uri: Url, text: &str) -> Vec<Diagnostic> {
Expand Down