From e5c9b49c833d24755d016021b45818721c75754f Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Mon, 13 Sep 2021 18:09:38 -0700 Subject: [PATCH] [examples] write labels.mdesk with examples of labels --- docs/metadesk_reference.mdesk | 2 +- examples/mdesk_files/labels.mdesk | 96 +++++++++++++++++++++++++++++++ intro_notes.txt | 2 +- source/md.c | 7 ++- source/md.h | 5 +- tests/sanity_tests.c | 8 +-- 6 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 examples/mdesk_files/labels.mdesk diff --git a/docs/metadesk_reference.mdesk b/docs/metadesk_reference.mdesk index 8ac2a23..2d134d6 100644 --- a/docs/metadesk_reference.mdesk +++ b/docs/metadesk_reference.mdesk @@ -322,7 +322,7 @@ main: @doc("This is a string literal that used triplets (three of its boundary characters in a row, on either side) to mark its boundaries, making it multiline.") StringTriplet, - @doc("The label on this node comes from a token with the @code MD_TokenKind_NumericLiteral kind.") + @doc("The label on this node comes from a token with the @code MD_TokenKind_Numeric kind.") Numeric, @doc("The label on this node comes from a token with the @code MD_TokenKind_Identifier kind.") Identifier, diff --git a/examples/mdesk_files/labels.mdesk b/examples/mdesk_files/labels.mdesk new file mode 100644 index 0000000..2219646 --- /dev/null +++ b/examples/mdesk_files/labels.mdesk @@ -0,0 +1,96 @@ +//////////////////////////////// +// Identifiers (MD_NodeFlag_Identifier) +abc _foo_ x123 +almostAnyCIdentifier // not including $ + +//////////////////////////////// +// Numerics (MD_NodeFlag_Numeric) +123 0x123ABC 0b10101 123.456 +0.123 123_456_789 +123abc456xyz +123e+100 +456E-100 +789e+E-e+E-100100xyz + +//////////////////////////////// +// String Literals (MD_NodeFlag_StringLiteral) + +//(MD_NodeFlag_StringDoubleQuote) +"Hello World" +" 'Hello World` " +"\"Hello World\"" +"\\\a\b\c\d\e\f\"" +"" + +//(MD_NodeFlag_StringSingleQuote) +'Hello World' +' "Hello World` ' +'' + +//(MD_NodeFlag_StringTick) +`Hello World` +` "Hello World' ` +`` + +//////////////////////////////// +// Multi-Line String Literals (MD_NodeFlag_StringLiteral|MD_NodeFlag_StringTriplet) + +//(MD_NodeFlag_StringDoubleQuote) +""" +This string can go on +for as many lines as I want. +And it can say things like +"Hello World" +or you know, whatever. +""" + +//(MD_NodeFlag_StringSingleQuote) +''' +Multi-line strings can start +with a triplet of any of the +string markers. +''' + +//(MD_NodeFlag_Tick) +``` +``` + +//////////////////////////////// +// Symbols (MD_NodeFlag_Symbol) ++ - * / = & ^ % ! | ? < > . ~ +$ +++ -- ** == && ^^ %% !! || ?? +<< >> .. ~~ $$ -> <- => <= ?. ++= -= *= /= |= &= ^= %= != |= +... ++- +++++++ ??? !? $! $$$ <-> ++-*/=&^%~|?<>.~$ + +/* +** A few punctuation characters are not counted with the rest +** of the symbol characters. These are the 'reserved' characters: +** ( ) { } [ ] : ; , @ # \ +** +** And the sequences // and /* start comments so no symbol +** can start with either of those pairs. +** +** Block comments are nested */ +*/ + + +//////////////////////////////// +// Identifiers, Numbers, Strings, and Symbols are the different kinds +// of 'labels' in Metadesk. They can all form main metadesk nodes: + +_foo_: { bar123456; _123; } + +0: (1.0.1.1, 1.2.0.0) + +``` +#include +int main(){ + printf("Hello World\n"); +} +```: "Hello World" + +..: . + . diff --git a/intro_notes.txt b/intro_notes.txt index d8f0c7a..9f50b6d 100644 --- a/intro_notes.txt +++ b/intro_notes.txt @@ -19,7 +19,7 @@ Example Programs: Example Metadesk Files: [x] Hello world -[ ] identifiers, numbers, strings, and symbols +[x] identifiers, numbers, strings, and symbols [ ] sets, seperators, labels, and tags diff --git a/source/md.c b/source/md.c index c72e1e7..945257c 100644 --- a/source/md.c +++ b/source/md.c @@ -619,7 +619,7 @@ MD_CharIsUnreservedSymbol(MD_u8 c) return (c == '~' || c == '!' || c == '$' || c == '%' || c == '^' || c == '&' || c == '*' || c == '-' || c == '=' || c == '+' || c == '<' || c == '.' || c == '>' || c == '/' || c == '?' || - c == '|' || c == '\\'); + c == '|'); } MD_FUNCTION_IMPL MD_b32 @@ -1891,7 +1891,7 @@ MD_TokenFromString(MD_String8 string) }break; - // NOTE(allen): Identifiers, Numbers, Operators + // NOTE(allen): Identifiers, Numbers, Symbols default: { if (MD_CharIsAlpha(*at) || *at == '_') @@ -1905,7 +1905,7 @@ MD_TokenFromString(MD_String8 string) else if (MD_CharIsDigit(*at)) { token.node_flags |= MD_NodeFlag_Numeric; - token.kind = MD_TokenKind_NumericLiteral; + token.kind = MD_TokenKind_Numeric; at += 1; for (; at < one_past_last;){ @@ -1932,6 +1932,7 @@ MD_TokenFromString(MD_String8 string) { symbol_lex: + token.node_flags |= MD_NodeFlag_Symbol; token.kind = MD_TokenKind_Symbol; at += 1; MD_TokenizerScan(MD_CharIsUnreservedSymbol(*at)); diff --git a/source/md.h b/source/md.h index 6d955d7..f62b8a2 100644 --- a/source/md.h +++ b/source/md.h @@ -584,6 +584,7 @@ enum MD_NodeFlag_Numeric = (1<<14), MD_NodeFlag_Identifier = (1<<15), MD_NodeFlag_StringLiteral = (1<<16), + MD_NodeFlag_Symbol = (1<<17), }; typedef struct MD_Node MD_Node; @@ -670,7 +671,7 @@ typedef MD_u32 MD_TokenKind; enum { MD_TokenKind_Identifier = (1<<0), - MD_TokenKind_NumericLiteral = (1<<1), + MD_TokenKind_Numeric = (1<<1), MD_TokenKind_StringLiteral = (1<<2), MD_TokenKind_Symbol = (1<<3), MD_TokenKind_Reserved = (1<<4), @@ -692,7 +693,7 @@ enum MD_TokenGroup_Whitespace), MD_TokenGroup_Regular = ~MD_TokenGroup_Irregular, MD_TokenGroup_Label = (MD_TokenKind_Identifier| - MD_TokenKind_NumericLiteral| + MD_TokenKind_Numeric| MD_TokenKind_StringLiteral| MD_TokenKind_Symbol), MD_TokenGroup_Error = (MD_TokenKind_BrokenComment| diff --git a/tests/sanity_tests.c b/tests/sanity_tests.c index 3951c32..5a42fdf 100644 --- a/tests/sanity_tests.c +++ b/tests/sanity_tests.c @@ -104,15 +104,15 @@ int main(void) TestResult(TokenMatch(tokens[1], MD_S8Lit(" "), MD_TokenKind_Whitespace)); TestResult(TokenMatch(tokens[2], MD_S8Lit("def"), MD_TokenKind_Identifier)); TestResult(TokenMatch(tokens[3], MD_S8Lit(" "), MD_TokenKind_Whitespace)); - TestResult(TokenMatch(tokens[4], MD_S8Lit("123"), MD_TokenKind_NumericLiteral)); + TestResult(TokenMatch(tokens[4], MD_S8Lit("123"), MD_TokenKind_Numeric)); TestResult(TokenMatch(tokens[5], MD_S8Lit(" "), MD_TokenKind_Whitespace)); - TestResult(TokenMatch(tokens[6], MD_S8Lit("456"), MD_TokenKind_NumericLiteral)); + TestResult(TokenMatch(tokens[6], MD_S8Lit("456"), MD_TokenKind_Numeric)); TestResult(TokenMatch(tokens[7], MD_S8Lit(" "), MD_TokenKind_Whitespace)); - TestResult(TokenMatch(tokens[8], MD_S8Lit("123_456"), MD_TokenKind_NumericLiteral)); + TestResult(TokenMatch(tokens[8], MD_S8Lit("123_456"), MD_TokenKind_Numeric)); TestResult(TokenMatch(tokens[9], MD_S8Lit(" "), MD_TokenKind_Whitespace)); TestResult(TokenMatch(tokens[10], MD_S8Lit("abc123"), MD_TokenKind_Identifier)); TestResult(TokenMatch(tokens[11], MD_S8Lit(" "), MD_TokenKind_Whitespace)); - TestResult(TokenMatch(tokens[12], MD_S8Lit("123abc"), MD_TokenKind_NumericLiteral)); + TestResult(TokenMatch(tokens[12], MD_S8Lit("123abc"), MD_TokenKind_Numeric)); TestResult(TokenMatch(tokens[13], MD_S8Lit(" "), MD_TokenKind_Whitespace)); TestResult(TokenMatch(tokens[14], MD_S8Lit("+-*"), MD_TokenKind_Symbol)); }