Skip to content

Commit

Permalink
feat: support for lsp syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Dec 16, 2023
1 parent 4bff00d commit 8e7d9ea
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
English | [简体中文](./README.zh-CN.md)
> This project is IEC-61131-3 Compiler implementation for technologies research.
![x](./screenshots/Screenshot_20231209_230838.png)
<p align="center">
<img src="./screenshots/Screenshot_20231216_233926.png"><br>
LSP-based syntax highlighting
</p>

<p align="center">
<img src="./screenshots/Screenshot_20231209_230838.png"><br>
A compiler data viewer tools UI
</p>

### TODO-List
- [ ] LSP implementation
Expand Down
1 change: 1 addition & 0 deletions lib/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ impl<'input> StLexer<'input> {
return self.parse_floating(tok, s);
}
_ => {
tok.length = s.len();
tok.tok = Tok::Literal(LiteralValue::UInt(s.parse().unwrap()));
return Some(Ok(tok));
}
Expand Down
6 changes: 4 additions & 2 deletions lib/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ impl Tok {
pub fn is_operator(&self) -> bool {
matches!(
self,
Tok::LessEqual
| Tok::Less
Tok::Less
| Tok::LessEqual
| Tok::Greater
| Tok::GreaterEqual
| Tok::Equal
| Tok::NotEqual
| Tok::Plus
| Tok::Minus
| Tok::Division
Expand Down
44 changes: 40 additions & 4 deletions lsp/src/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde_json::Value;
use stc::parser::StLexerBuilder;
use stc::parser::{StLexerBuilder, Tok};
use tower_lsp::jsonrpc;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
Expand All @@ -25,8 +25,11 @@ impl LanguageServer for StcLsp {
},
legend: SemanticTokensLegend {
token_types: vec![
SemanticTokenType::KEYWORD,
SemanticTokenType::VARIABLE,
SemanticTokenType::NUMBER,
SemanticTokenType::STRING,
SemanticTokenType::OPERATOR,
SemanticTokenType::FUNCTION,
],
token_modifiers: vec![],
Expand Down Expand Up @@ -92,11 +95,44 @@ impl LanguageServer for StcLsp {
let lexer = StLexerBuilder::new()
.build_file(params.text_document.uri.path())
.map_err(|_| jsonrpc::Error::invalid_request())?;
for tok in lexer {
info!("{:?}", tok.unwrap().tok);

let mut last_line = 0;
let mut last_offset = 0;
let mut tokens: Vec<SemanticToken> = vec![];
for tok in lexer.flatten() {
if tok.pos.line != last_line {
last_offset = 0;
}

let tok_type = match tok.tok {
Tok::Identifier(_) => 1,
Tok::Literal(_) => 2,
Tok::String => 3,
op if op.is_operator() => 4,
_ => 0,
};

let token = SemanticToken {
delta_line: (tok.pos.line - last_line) as u32,
delta_start: (tok.pos.offset - last_offset) as u32,
length: tok.length as u32,
token_type: tok_type,
..Default::default()
};
tokens.push(token);

last_line = tok.pos.line;
last_offset = tok.pos.offset;
}

Ok(None)
for x in &tokens {
trace!("{:?}", x);
}

Ok(Some(SemanticTokensResult::Tokens(SemanticTokens {
result_id: None,
data: tokens,
})))
}

async fn semantic_tokens_full_delta(
Expand Down
Binary file added screenshots/Screenshot_20231216_233926.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8e7d9ea

Please sign in to comment.