Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
139 changes: 139 additions & 0 deletions lexers/embedded/microcad.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<lexer>
<config>
<name>microcad</name>
<alias>µcad</alias>
<filename>*.µcad</filename>
<filename>*.ucad</filename>
<filename>*.mcad</filename>
<mime_type>text/microcad</mime_type>
<ensure_nl>true</ensure_nl>
</config>

<rules>
<!-- Root state -->
<state name="root">
<rule pattern="\n">
<token type="TextWhitespace"/>
</rule>
<rule pattern="\s+">
<token type="TextWhitespace"/>
</rule>

<!-- COMMENTS -->
<rule pattern="///.*">
<token type="LiteralStringDoc"/>
</rule>

<rule pattern="//.*">
<token type="CommentSingle"/>
</rule>

<rule pattern="/\*.*">
<token type="CommentMultiline"/>
<push state="comment"/>
</rule>

<!-- KEYWORDS -->
<rule pattern="\b(pub|sketch|part|op|mod|use|fn|const|prop|init|return|if|else|mat|__builtin|or|and|not|as)\b">
<token type="Keyword"/>
</rule>

<!-- TYPES -->
<rule pattern="\b(Integer|Scalar|String|Color|Length|Area|Volume|Angle|Weight|Density|Bool|Matrix[0-9])\b">
<token type="KeywordType"/>
</rule>

<!-- FUNCTION NAMES -->
<rule pattern="\b([a-z_][a-zA-Z0-9_]*)\s*(?=\()">
<token type="NameFunction"/>
</rule>

<!-- OPERATORS -->
<rule pattern="[+\-*/%&amp;|&lt;&gt;^!@=]">
<token type="Operator"/>
</rule>

<!-- NUMBERS + UNITS -->
<rule pattern="\b(([0-9]+(\.[0-9]+)?)(%|m²|cm²|mm²|µm²|in²|ft²|yd²|m³|cm³|mm³|µm³|in³|ft³|yd³|ml|cl|l|µl|cm|mm|m|µm|in|ft|yd|deg|°|grad|turn|rad|g|kg|lb|oz)?)|true|false\b">
<token type="LiteralNumber"/>
</rule>

<!-- NAMESPACES -->
<rule pattern="([a-z_][a-z0-9_]*)(::)">
<bygroups>
<token type="NameNamespace"/>
<token type="Operator"/>
</bygroups>
</rule>

<!-- CONSTANTS (ALL CAPS) -->
<rule pattern="\b([A-Z_][A-Z0-9_]*)\b">
<token type="NameConstant"/>
</rule>

<!-- CUSTOM TYPES (Capitalized identifiers) -->
<rule pattern="\b([A-Z_][a-zA-Z0-9_]*)\b">
<token type="NameClass"/>
</rule>

<!-- VARIABLES -->
<rule pattern="\b([a-z_][a-zA-Z0-9_]*)\b">
<token type="Name"/>
</rule>

<!-- STRINGS -->
<rule pattern="&quot;[^&quot;]*&quot;">
<token type="LiteralString"/>
</rule>

<!-- PAREN GROUP -->
<rule pattern="[{}()\[\],.;:]">
<token type="Punctuation"/>
</rule>


<!-- BRACE GROUP -->
<rule pattern="\{">
<token type="Punctuation"/>
<push state="brace"/>
</rule>

<rule pattern="[\}\)]">
<token type="Punctuation"/>
</rule>

</state>

<!-- Parenthesized expression -->
<state name="paren">
<rule pattern="\)">
<token type="Punctuation"/>
<pop/>
</rule>
<rule>
<include state="root"/>
</rule>
</state>

<!-- Braced expression -->
<state name="brace">
<rule pattern="\}">
<token type="Punctuation"/>
<pop/>
</rule>
<rule>
<include state="root"/>
</rule>
</state>

<state name="comment">
<!-- End of comment -->
<rule pattern="\*/">
<token type="CommentMultiline"/>
<pop/>
</rule>
</state>


</rules>
</lexer>
16 changes: 16 additions & 0 deletions lexers/testdata/microcad.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright © 2025 The µcad authors <[email protected]>
// SPDX-License-Identifier: AGPL-3.0-or-later

use std::math::*;
use std::ops::*;
use std::geo3d::*;

/// A CSG cube.
part CsgCube(size: Length) {
s = size / sqrt(2.1); /* Inline comment */
body = Sphere(r = s) & Cube(size);
holes = Cylinder(h = size, d = s).orient([X,Y,Z]);
body - holes;
}

CsgCube(50mm);
121 changes: 121 additions & 0 deletions lexers/testdata/microcad.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
[
{"type":"CommentSingle","value":"// Copyright © 2025 The µcad authors \[email protected]\u003e"},
{"type":"TextWhitespace","value":"\n"},
{"type":"CommentSingle","value":"// SPDX-License-Identifier: AGPL-3.0-or-later"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"Keyword","value":"use"},
{"type":"TextWhitespace","value":" "},
{"type":"NameNamespace","value":"std"},
{"type":"Operator","value":"::"},
{"type":"NameNamespace","value":"math"},
{"type":"Operator","value":"::*"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Keyword","value":"use"},
{"type":"TextWhitespace","value":" "},
{"type":"NameNamespace","value":"std"},
{"type":"Operator","value":"::"},
{"type":"NameNamespace","value":"ops"},
{"type":"Operator","value":"::*"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Keyword","value":"use"},
{"type":"TextWhitespace","value":" "},
{"type":"NameNamespace","value":"std"},
{"type":"Operator","value":"::"},
{"type":"NameNamespace","value":"geo3d"},
{"type":"Operator","value":"::*"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"LiteralStringDoc","value":"/// A CSG cube."},
{"type":"TextWhitespace","value":"\n"},
{"type":"Keyword","value":"part"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"CsgCube"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"size"},
{"type":"Punctuation","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"Length"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"s"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"size"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"/"},
{"type":"TextWhitespace","value":" "},
{"type":"NameFunction","value":"sqrt"},
{"type":"Punctuation","value":"("},
{"type":"LiteralNumber","value":"2.1"},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":" "},
{"type":"CommentMultiline","value":"/* Inline comment */"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"body"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Sphere"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"r"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"s"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"\u0026"},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Cube"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"size"},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"holes"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"NameClass","value":"Cylinder"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"h"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"size"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"d"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"s"},
{"type":"Punctuation","value":")."},
{"type":"NameFunction","value":"orient"},
{"type":"Punctuation","value":"(["},
{"type":"NameConstant","value":"X"},
{"type":"Punctuation","value":","},
{"type":"NameConstant","value":"Y"},
{"type":"Punctuation","value":","},
{"type":"NameConstant","value":"Z"},
{"type":"Punctuation","value":"]);"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"body"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"-"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"holes"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"NameClass","value":"CsgCube"},
{"type":"Punctuation","value":"("},
{"type":"LiteralNumber","value":"50mm"},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n"}
]