From 79d2f993f994765a4e3e101e3d9af9d07e4fee19 Mon Sep 17 00:00:00 2001 From: webdevred <148627186+webdevred@users.noreply.github.com> Date: Fri, 20 Jun 2025 11:25:18 +0200 Subject: [PATCH 1/2] Add support for comments in jbfl --- examples/jbfl/complex.jbfl | 23 +++++++++++++++++++++-- examples/jbfl/minimal.jbfl | 2 ++ src/Parsing/DSL.hs | 17 ++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/examples/jbfl/complex.jbfl b/examples/jbfl/complex.jbfl index 6a57bac3..75c7b6e6 100644 --- a/examples/jbfl/complex.jbfl +++ b/examples/jbfl/complex.jbfl @@ -1,16 +1,35 @@ +// Apply default padding and decimal formatting to all node elements .*.nodes[*][*] { + // Show numbers with exactly 3 decimal places (e.g., 1.000) PadDecimals: 3; + + // Pad numeric values to 6 characters wide for alignment PadAmount: 6; } +// Special padding for the first element in each node list (vertex names) .*.nodes[*][0] { + /* + Vertex names can be up to 8 characters long, + so we pad them to width 8 for neat column alignment. + This improves readability by lining up numeric columns. + */ PadAmount: 8; } .*.nodes[*] { - NoComplexNewLine : true; + /* + Prevent inserting newlines inside node elements with complex children. + Disable complex newlines here to keep nested structures inline. + This avoids cluttering the output with unnecessary line breaks, + making it easier to scan. + */ + NoComplexNewLine: true; } +// Flexbodies typically contain nested lists representing grouped geometry or materials +// Keep complex nested content inline rather than breaking into multiple lines +// This makes output more compact and easier to scan given typical data patterns .*.flexbodies[*] { - NoComplexNewLine : true; + NoComplexNewLine: true; } diff --git a/examples/jbfl/minimal.jbfl b/examples/jbfl/minimal.jbfl index 603a4a01..75e636bc 100644 --- a/examples/jbfl/minimal.jbfl +++ b/examples/jbfl/minimal.jbfl @@ -1,8 +1,10 @@ .*.nodes[*][*] { + // Pad all node values to 8 chars and force 3 decimals (e.g., 1.000) PadAmount: 8; PadDecimals: 3; } +// Keep flexbodies inline (no extra newlines for nested content) .*.flexbodies[*] { NoComplexNewLine : true; } diff --git a/src/Parsing/DSL.hs b/src/Parsing/DSL.hs index adb20af0..3028538e 100644 --- a/src/Parsing/DSL.hs +++ b/src/Parsing/DSL.hs @@ -29,7 +29,11 @@ import Data.Set qualified as S (fromList) import Data.Text qualified as T import Text.Megaparsec qualified as MP import Text.Megaparsec.Byte qualified as B -import Text.Megaparsec.Byte.Lexer qualified as L (decimal) +import Text.Megaparsec.Byte.Lexer qualified as L ( + decimal, + skipBlockComment, + skipLineComment, + ) objectKeyParser :: Parser NodePatternSelector objectKeyParser = byteChar '.' *> key @@ -76,6 +80,7 @@ propertyParser (SomeKey key) = do skipWhiteSpace val <- parseValueForKey key separatorParser + skipComment let prop = SomeProperty key in pure (SomeKey key, prop val) @@ -84,9 +89,14 @@ parseValueForKey NoComplexNewLine = parseBool "bool" parseValueForKey PadAmount = L.decimal "integer" parseValueForKey PadDecimals = L.decimal "integer" +skipComment :: Parser () +skipComment = void . MP.many $ comment <* skipWhiteSpace + where + comment = tryParsers [L.skipLineComment "//", L.skipBlockComment "/*" "*/"] + keyPropertyPairParser :: Parser (SomeKey, SomeProperty) keyPropertyPairParser = do - skipWhiteSpace + skipComment offset <- MP.getOffset key <- MP.label "property name" $ @@ -111,12 +121,13 @@ ruleParser = do skipWhiteSpace props <- MP.some (MP.try keyPropertyPairParser) _ <- byteChar '}' + skipWhiteSpace pure (pat, M.fromList props) ruleSetParser :: Parser RuleSet ruleSetParser = RuleSet . M.fromListWith M.union <$> MP.some singleRuleSet where - singleRuleSet = skipWhiteSpace *> ruleParser <* skipWhiteSpace + singleRuleSet = skipComment *> ruleParser <* skipComment parseDSL :: ByteString -> Either Text RuleSet parseDSL input From 87c41bfe3656b86a9971b690f72ddf4e9690fa5e Mon Sep 17 00:00:00 2001 From: webdevred <148627186+webdevred@users.noreply.github.com> Date: Fri, 20 Jun 2025 11:26:45 +0200 Subject: [PATCH 2/2] Move whitespace skipping from patternSelectorParser to patternParser --- src/Parsing/DSL.hs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Parsing/DSL.hs b/src/Parsing/DSL.hs index 3028538e..bdcaa329 100644 --- a/src/Parsing/DSL.hs +++ b/src/Parsing/DSL.hs @@ -54,19 +54,19 @@ arrayIndexParser = byteChar '[' *> index <* byteChar ']' index = Selector . ArrayIndex <$> L.decimal patternSelectorParser :: Parser NodePatternSelector -patternSelectorParser = skipWhiteSpace *> anySel <* skipWhiteSpace - where - anySel = - tryParsers - [ B.string ".*" $> AnyObjectKey - , B.string "[*]" $> AnyArrayIndex - , objectIndexParser "object index" - , objectKeyParser "object key" - , arrayIndexParser "array index" - ] +patternSelectorParser = + tryParsers + [ B.string ".*" $> AnyObjectKey + , B.string "[*]" $> AnyArrayIndex + , objectIndexParser "object index" + , objectKeyParser "object key" + , arrayIndexParser "array index" + ] patternParser :: Parser NodePattern -patternParser = NodePattern . Seq.fromList <$> MP.some patternSelectorParser +patternParser = skipWhiteSpace *> patternSelectors <* skipWhiteSpace + where + patternSelectors = NodePattern . Seq.fromList <$> MP.some patternSelectorParser tryDecodeKey :: [Word8] -> (Text -> Maybe SomeKey) -> Maybe SomeKey tryDecodeKey bs f =