Skip to content
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

feat: Typst Language Support #289

Closed
wants to merge 16 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
88 changes: 61 additions & 27 deletions harper-core/src/parsers/typst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ fn parse_english(
parser: &mut PlainEnglish,
span: &typst_syntax::Span,
) -> Option<Vec<Token>> {
let res = parser
.parse_str(str.into())
.into_iter()
.map(|mut t| {
t.span.push_by(doc.range(*span).unwrap().start);
t
})
.collect_vec();
Some(res)
let offset = doc.range(*span).unwrap().start;
Some(
parser
.parse_str(str.into())
.into_iter()
.map(|mut t| {
t.span.push_by(offset);
t
})
.collect_vec(),
)
}

fn parse_dict(
Expand All @@ -73,8 +75,7 @@ fn parse_dict(
dict.filter_map(|di| match di {
typst_syntax::ast::DictItem::Named(named) => merge_expr!(
constant_token!(doc, named.name(), TokenKind::Word(WordMetadata::default())),
map_token(named.expr(), doc, parser),
parse_pattern(named.pattern(), doc, parser)
map_token(named.expr(), doc, parser)
),
typst_syntax::ast::DictItem::Keyed(keyed) => merge_expr!(
map_token(keyed.key(), doc, parser),
Expand Down Expand Up @@ -241,7 +242,15 @@ fn map_token(
),
Expr::FuncCall(a) => constant_token!(doc, a, TokenKind::Unlintable),
Expr::Closure(a) => constant_token!(doc, a, TokenKind::Unlintable),
Expr::Let(let_binding) => let_binding.init().and_then(|e| map_token(e, doc, parser)),
Expr::Let(let_binding) => merge_expr!(
match let_binding.kind() {
typst_syntax::ast::LetBindingKind::Normal(pattern) =>
parse_pattern(pattern, doc, parser),
typst_syntax::ast::LetBindingKind::Closure(ident) =>
constant_token!(doc, ident, TokenKind::Word(WordMetadata::default())),
},
let_binding.init().and_then(|e| map_token(e, doc, parser))
),
Expr::DestructAssign(destruct_assignment) => {
map_token(destruct_assignment.value(), doc, parser)
}
Expand Down Expand Up @@ -354,19 +363,18 @@ impl Parser for Typst {

#[cfg(test)]
mod tests {
use itertools::Itertools;
use ordered_float::OrderedFloat;

use super::Typst;
use crate::{parsers::StrParser, NounData, Punctuation, TokenKind, WordMetadata};

#[test]
fn conjunction() {
let source = r"doesn't";
let source = "doesn't";

let tokens = Typst.parse_str(source);

let token_kinds = tokens.iter().map(|t| t.kind).collect::<Vec<_>>();

dbg!(&token_kinds);

assert_eq!(token_kinds.len(), 1);
Expand All @@ -375,12 +383,10 @@ mod tests {

#[test]
fn possessive() {
let source = r"person's";
let source = "person's";

let tokens = Typst.parse_str(source);

let token_kinds = tokens.iter().map(|t| t.kind).collect::<Vec<_>>();

dbg!(&token_kinds);

assert_eq!(token_kinds.len(), 1);
Expand All @@ -400,12 +406,10 @@ mod tests {

#[test]
fn number() {
let source = r"12 is larger than 11, but much less than 11!";
let source = "12 is larger than 11, but much less than 11!";

let tokens = Typst.parse_str(source);

let token_kinds = tokens.iter().map(|t| t.kind).collect::<Vec<_>>();

dbg!(&token_kinds);

assert!(matches!(
Expand Down Expand Up @@ -438,12 +442,10 @@ mod tests {

#[test]
fn math_unlintable() {
let source = r"$12 > 11$, $12 << 11!$";
let source = "$12 > 11$, $12 << 11!$";

let tokens = Typst.parse_str(source);

let token_kinds = tokens.iter().map(|t| t.kind).collect::<Vec<_>>();

dbg!(&token_kinds);

assert!(matches!(
Expand All @@ -458,13 +460,45 @@ mod tests {
}

#[test]
fn sentence() {
let source = r"This is a sentence, it does not have any particularly interesting elements of the typst syntax.";
fn dict_parsing() {
let source = r#"#let dict = (
name: "Typst",
born: 2019,
)"#;

let tokens = Typst.parse_str(source);

let token_kinds = tokens.iter().map(|t| t.kind).collect::<Vec<_>>();
dbg!(&token_kinds);

let typst_document = typst_syntax::Source::detached(source);
let typst_tree = <typst_syntax::ast::Markup as typst_syntax::ast::AstNode>::from_untyped(
typst_document.root(),
)
.expect("Unable to create typst document from parsed tree!");
dbg!(typst_tree.exprs().collect_vec());
Comment on lines +472 to +478
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove before opening PR.


let charslice = source.chars().collect_vec();
assert_eq!(tokens[2].span.get_content_string(&charslice), "Typst");
assert!(matches!(
token_kinds.as_slice(),
&[
TokenKind::Word(_),
TokenKind::Word(_),
TokenKind::Punctuation(Punctuation::Quote { .. }),
TokenKind::Word(_),
TokenKind::Punctuation(Punctuation::Quote { .. }),
TokenKind::Word(_),
TokenKind::Number(OrderedFloat(2019.0), None),
]
))
}

#[test]
fn sentence() {
let source = "This is a sentence, it does not have any particularly interesting elements of the typst syntax.";

let tokens = Typst.parse_str(source);
let token_kinds = tokens.iter().map(|t| t.kind).collect::<Vec<_>>();
dbg!(&token_kinds);

assert!(matches!(
Expand Down
Loading