manen

Fancy Lua REPL
Log | Files | Refs | README | LICENSE

commit 6aa6f9b74ff472e22886bc1dcba67b1f10e591fa
parent 70724afa2ddfd2be137b649857f0e1e47deed5e1
Author: Sylvia Ivory <git@sivory.net>
Date:   Sat, 28 Jun 2025 23:24:29 -0700

Change parser depending on Lua version

Diffstat:
Msrc/completion.rs | 8+++++---
Msrc/parse.rs | 42+++++++++++++++++++++++++++++++++++++++---
2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/src/completion.rs b/src/completion.rs @@ -1,10 +1,12 @@ use emmylua_parser::{ - LuaAst, LuaAstNode, LuaAstToken, LuaBlock, LuaNameExpr, LuaParser, LuaSyntaxTree, ParserConfig, + LuaAst, LuaAstNode, LuaAstToken, LuaBlock, LuaNameExpr, LuaParser, LuaSyntaxTree, }; use mlua::prelude::*; use reedline::{Completer, Span, Suggestion}; use rowan::TextRange; +use crate::parse; + #[derive(Debug)] struct Variable { range: TextRange, @@ -29,14 +31,14 @@ impl LuaCompleter { pub fn new(lua: Lua) -> Self { Self { lua, - tree: LuaParser::parse("", ParserConfig::default()), + tree: LuaParser::parse("", parse::config()), scopes: Vec::new(), text: String::new(), } } fn refresh_tree(&mut self, text: &str) { - self.tree = LuaParser::parse(text, ParserConfig::default()); + self.tree = LuaParser::parse(text, parse::config()); self.text = text.to_string(); self.scopes = self.resolve_scopes(); } diff --git a/src/parse.rs b/src/parse.rs @@ -1,11 +1,47 @@ use emmylua_parser::{ - LuaAst, LuaAstNode, LuaKind, LuaParser, LuaSyntaxKind, LuaSyntaxNode, LuaSyntaxToken, - LuaSyntaxTree, LuaTokenKind, ParserConfig, + LuaAst, LuaAstNode, LuaKind, LuaLanguageLevel, LuaParser, LuaSyntaxKind, LuaSyntaxNode, + LuaSyntaxToken, LuaSyntaxTree, LuaTokenKind, ParserConfig, }; use nu_ansi_term::{Color, Style}; use reedline::StyledText; use rowan::WalkEvent; +#[cfg(feature = "lua54")] +pub fn config<'cache>() -> ParserConfig<'cache> { + ParserConfig::with_level(LuaLanguageLevel::Lua54) +} + +#[cfg(feature = "lua53")] +pub fn config<'cache>() -> ParserConfig<'cache> { + ParserConfig::with_level(LuaLanguageLevel::Lua53) +} + +#[cfg(feature = "lua52")] +pub fn config<'cache>() -> ParserConfig<'cache> { + ParserConfig::with_level(LuaLanguageLevel::Lua52) +} + +#[cfg(feature = "lua51")] +pub fn config<'cache>() -> ParserConfig<'cache> { + ParserConfig::with_level(LuaLanguageLevel::Lua51) +} + +#[cfg(any(feature = "luajit", feature = "luajit52"))] +pub fn config<'cache>() -> ParserConfig<'cache> { + ParserConfig::with_level(LuaLanguageLevel::LuaJIT) +} + +#[cfg(any(feature = "luajit", feature = "luajit52"))] +pub fn config<'cache>() -> ParserConfig<'cache> { + ParserConfig::with_level(LuaLanguageLevel::LuaJIT) +} + +// not entirely accurate but this will do for now +#[cfg(feature = "luau")] +pub fn config<'cache>() -> ParserConfig<'cache> { + ParserConfig::with_level(LuaLanguageLevel::Lua51) +} + fn node_name(node: &LuaAst) -> Option<&'static str> { match node { LuaAst::LuaChunk(_) => Some("chunk"), @@ -309,7 +345,7 @@ pub struct LuaHighlighter; impl reedline::Highlighter for LuaHighlighter { fn highlight(&self, line: &str, _cursor: usize) -> StyledText { - let tree = LuaParser::parse(line, ParserConfig::default()); + let tree = LuaParser::parse(line, config()); let root = tree.get_red_root(); let mut text = StyledText::new();