mirror of
https://github.com/joshuadavidthomas/django-language-server.git
synced 2025-08-03 17:48:20 +00:00
refactor parser a bit (#108)
Some checks failed
release / linux (map[runner:ubuntu-22.04 target:armv7]) (push) Failing after 3s
release / linux (map[runner:ubuntu-22.04 target:s390x]) (push) Failing after 3s
release / linux (map[runner:ubuntu-22.04 target:x86_64]) (push) Failing after 3s
release / linux (map[runner:ubuntu-22.04 target:ppc64le]) (push) Failing after 2s
release / linux (map[runner:ubuntu-22.04 target:x86]) (push) Failing after 3s
release / musllinux (map[runner:ubuntu-22.04 target:armv7]) (push) Failing after 3s
release / musllinux (map[runner:ubuntu-22.04 target:x86]) (push) Failing after 3s
release / musllinux (map[runner:ubuntu-22.04 target:x86_64]) (push) Failing after 3s
release / test (push) Has been skipped
release / musllinux (map[runner:ubuntu-22.04 target:aarch64]) (push) Failing after 3s
release / linux (map[runner:ubuntu-22.04 target:aarch64]) (push) Failing after 5s
lint / pre-commit (push) Has been cancelled
release / windows (map[runner:windows-latest target:x64]) (push) Has been cancelled
release / windows (map[runner:windows-latest target:x86]) (push) Has been cancelled
release / macos (map[runner:macos-13 target:x86_64]) (push) Has been cancelled
release / macos (map[runner:macos-14 target:aarch64]) (push) Has been cancelled
release / sdist (push) Has been cancelled
release / release (push) Has been cancelled
Some checks failed
release / linux (map[runner:ubuntu-22.04 target:armv7]) (push) Failing after 3s
release / linux (map[runner:ubuntu-22.04 target:s390x]) (push) Failing after 3s
release / linux (map[runner:ubuntu-22.04 target:x86_64]) (push) Failing after 3s
release / linux (map[runner:ubuntu-22.04 target:ppc64le]) (push) Failing after 2s
release / linux (map[runner:ubuntu-22.04 target:x86]) (push) Failing after 3s
release / musllinux (map[runner:ubuntu-22.04 target:armv7]) (push) Failing after 3s
release / musllinux (map[runner:ubuntu-22.04 target:x86]) (push) Failing after 3s
release / musllinux (map[runner:ubuntu-22.04 target:x86_64]) (push) Failing after 3s
release / test (push) Has been skipped
release / musllinux (map[runner:ubuntu-22.04 target:aarch64]) (push) Failing after 3s
release / linux (map[runner:ubuntu-22.04 target:aarch64]) (push) Failing after 5s
lint / pre-commit (push) Has been cancelled
release / windows (map[runner:windows-latest target:x64]) (push) Has been cancelled
release / windows (map[runner:windows-latest target:x86]) (push) Has been cancelled
release / macos (map[runner:macos-13 target:x86_64]) (push) Has been cancelled
release / macos (map[runner:macos-14 target:aarch64]) (push) Has been cancelled
release / sdist (push) Has been cancelled
release / release (push) Has been cancelled
This commit is contained in:
parent
980983e4f3
commit
9fcf71f6af
29 changed files with 61 additions and 68 deletions
|
@ -1,16 +1,16 @@
|
|||
use crate::tokens::Token;
|
||||
use crate::tokens::{Token, TokenStream, TokenType};
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Clone, Default, Debug, Serialize)]
|
||||
pub struct NodeList {
|
||||
nodes: Vec<Node>,
|
||||
#[derive(Clone, Debug, Default, Serialize)]
|
||||
pub struct Ast {
|
||||
nodelist: Vec<Node>,
|
||||
line_offsets: LineOffsets,
|
||||
}
|
||||
|
||||
impl NodeList {
|
||||
pub fn nodes(&self) -> &Vec<Node> {
|
||||
&self.nodes
|
||||
impl Ast {
|
||||
pub fn nodelist(&self) -> &Vec<Node> {
|
||||
&self.nodelist
|
||||
}
|
||||
|
||||
pub fn line_offsets(&self) -> &LineOffsets {
|
||||
|
@ -18,26 +18,25 @@ impl NodeList {
|
|||
}
|
||||
|
||||
pub fn add_node(&mut self, node: Node) {
|
||||
self.nodes.push(node);
|
||||
self.nodelist.push(node);
|
||||
}
|
||||
|
||||
pub fn set_line_offsets(&mut self, line_offsets: LineOffsets) {
|
||||
self.line_offsets = line_offsets
|
||||
}
|
||||
|
||||
pub fn finalize(&mut self) -> NodeList {
|
||||
self.clone()
|
||||
pub fn set_line_offsets(&mut self, tokens: &TokenStream) {
|
||||
for token in tokens.tokens() {
|
||||
if let TokenType::Newline = token.token_type() {
|
||||
if let Some(start) = token.start() {
|
||||
// Add offset for next line
|
||||
self.line_offsets.add_line(start + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Debug, Serialize)]
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct LineOffsets(pub Vec<u32>);
|
||||
|
||||
impl LineOffsets {
|
||||
pub fn new() -> Self {
|
||||
Self(vec![0])
|
||||
}
|
||||
|
||||
pub fn add_line(&mut self, offset: u32) {
|
||||
self.0.push(offset);
|
||||
}
|
||||
|
@ -67,6 +66,12 @@ impl LineOffsets {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for LineOffsets {
|
||||
fn default() -> Self {
|
||||
Self(vec![0])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub enum Node {
|
||||
Tag {
|
||||
|
@ -155,13 +160,13 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_new_starts_at_zero() {
|
||||
let offsets = LineOffsets::new();
|
||||
let offsets = LineOffsets::default();
|
||||
assert_eq!(offsets.position_to_line_col(0), (1, 0)); // Line 1, column 0
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_start_of_lines() {
|
||||
let mut offsets = LineOffsets::new();
|
||||
let mut offsets = LineOffsets::default();
|
||||
offsets.add_line(10); // Line 2 starts at offset 10
|
||||
offsets.add_line(25); // Line 3 starts at offset 25
|
||||
|
||||
|
@ -183,7 +188,7 @@ mod tests {
|
|||
assert!(errors.is_empty());
|
||||
|
||||
// Find the variable node
|
||||
let nodes = nodelist.nodes();
|
||||
let nodes = nodelist.nodelist();
|
||||
let var_node = nodes
|
||||
.iter()
|
||||
.find(|n| matches!(n, Node::Variable { .. }))
|
||||
|
|
|
@ -5,7 +5,7 @@ mod parser;
|
|||
mod tagspecs;
|
||||
mod tokens;
|
||||
|
||||
use ast::NodeList;
|
||||
use ast::Ast;
|
||||
pub use error::{to_lsp_diagnostic, QuickFix, TemplateError};
|
||||
|
||||
use lexer::Lexer;
|
||||
|
@ -18,7 +18,7 @@ pub use parser::{Parser, ParserError};
|
|||
///
|
||||
/// Returns a `Result` containing a tuple of `(Ast, Vec<ParserError>)` on success,
|
||||
/// or a `ParserError` on failure.
|
||||
pub fn parse_template(source: &str) -> Result<(NodeList, Vec<TemplateError>), TemplateError> {
|
||||
pub fn parse_template(source: &str) -> Result<(Ast, Vec<TemplateError>), TemplateError> {
|
||||
let tokens = Lexer::new(source)
|
||||
.tokenize()
|
||||
.map_err(|e| TemplateError::Lexer(e.to_string()))?;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::ast::{AstError, LineOffsets, Node, NodeList, Span};
|
||||
use crate::ast::{Ast, AstError, Node, Span};
|
||||
use crate::lexer::LexerError;
|
||||
use crate::tokens::{Token, TokenStream, TokenType};
|
||||
use thiserror::Error;
|
||||
|
@ -18,25 +18,14 @@ impl Parser {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse(&mut self) -> Result<(NodeList, Vec<ParserError>), ParserError> {
|
||||
let mut nodelist = NodeList::default();
|
||||
let mut line_offsets = LineOffsets::new();
|
||||
|
||||
for token in self.tokens.tokens() {
|
||||
if let TokenType::Newline = token.token_type() {
|
||||
if let Some(start) = token.start() {
|
||||
// Add offset for next line
|
||||
line_offsets.add_line(start + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.current = 0;
|
||||
pub fn parse(&mut self) -> Result<(Ast, Vec<ParserError>), ParserError> {
|
||||
let mut ast = Ast::default();
|
||||
ast.set_line_offsets(&self.tokens);
|
||||
|
||||
while !self.is_at_end() {
|
||||
match self.next_node() {
|
||||
Ok(node) => {
|
||||
nodelist.add_node(node);
|
||||
ast.add_node(node);
|
||||
}
|
||||
Err(err) => {
|
||||
if !self.is_at_end() {
|
||||
|
@ -47,8 +36,7 @@ impl Parser {
|
|||
}
|
||||
}
|
||||
|
||||
nodelist.set_line_offsets(line_offsets);
|
||||
Ok((nodelist.finalize(), std::mem::take(&mut self.errors)))
|
||||
Ok((ast.clone(), std::mem::take(&mut self.errors)))
|
||||
}
|
||||
|
||||
fn next_node(&mut self) -> Result<Node, ParserError> {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<!-- HTML comment -->"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Tag:
|
||||
name: if
|
||||
bits:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Tag:
|
||||
name: for
|
||||
bits:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Tag:
|
||||
name: if
|
||||
bits:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Tag:
|
||||
name: url
|
||||
bits:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Variable:
|
||||
var: user.name
|
||||
filters: []
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Variable:
|
||||
var: user.name
|
||||
filters:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Variable:
|
||||
var: value
|
||||
filters:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "Welcome,"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Tag:
|
||||
name: for
|
||||
bits:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<div class=\"container\">"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Tag:
|
||||
name: for
|
||||
bits:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Tag:
|
||||
name: if
|
||||
bits:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<div>"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<script>console.log('test');"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<style>body { color: blue;"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<!DOCTYPE html>"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<!DOCTYPE html>"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<div class=\"container\">Hello</div>"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<input type=\"text\" />"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<script type=\"text/javascript\">"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: "<style type=\"text/css\">"
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: hello
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: hello
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: hello
|
||||
span:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
source: crates/djls-templates/src/parser.rs
|
||||
expression: nodelist
|
||||
---
|
||||
nodes:
|
||||
nodelist:
|
||||
- Text:
|
||||
content: hello
|
||||
span:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue