sun::parsing
namespace · Source (opens in a new tab)
Turns source text into syntax trees and provides source formatting.
Turns Sun source text into tokens and syntax trees.
Classes
- sun::parsing::InterpolatedStringParser (public)
- sun::parsing::Lexer (public)
- sun::parsing::LoweringPass (public)
- sun::parsing::NFA (public)
- sun::parsing::DFA (public)
- sun::parsing::RegexParser (public)
- sun::parsing::Parser (public)
Structs
- sun::parsing::TokenInfo (public)
- sun::parsing::Token (public)
- sun::parsing::State (public)
- sun::parsing::RegexCapture (public)
- sun::parsing::TokenChar (public)
- sun::parsing::Comment (public)
Functions
- attachDocComments
- compoundToBinaryOp
- decodeUtf8
- docCommentAbove
- encodeUtf8
- formatProgram
- formatSource
- getKeywordSpelling
- getTokenInfo
- hexByte
- hexDigit
- isAssignmentOp
- isKeyword
- isPublicableStatementStart
- isScalarValue
- isSurrogate
- normalizeBody
- putchard
- simple
- utf8SequenceLength
attachDocComments
public · function · Source (opens in a new tab)
void sun::parsing::attachDocComments(sun::ast::BlockExprAST &program, const std::string &source)Store the doc comment of every declaration in the program on its node — functions, classes and their fields and methods, interfaces, enums and their variants, module-level variables — so it travels with the tree into .moon bundles and reaches editor tooling without the source at hand.
Attaches source documentation to declarations in a parsed program.
Related: sun::ast::BlockExprAST
compoundToBinaryOp
public · function · Source (opens in a new tab)
std::optional< TokenKind > sun::parsing::compoundToBinaryOp(TokenKind kind)Map compound-assignment token kinds (+=, -=, ...) to the underlying binary operator; nullopt for anything else.
Shared by the parser, semantic analysis, and codegen.
Related: TokenKind
decodeUtf8
public · function · Source (opens in a new tab)
std::optional< uint32_t > sun::parsing::decodeUtf8(std::string_view s, int &length)Decode the UTF-8 sequence at the start of s.
Returns the scalar value and writes the number of bytes consumed to length, or nullopt if the bytes are not well-formed UTF-8 (bad continuation byte, overlong form, surrogate, or out of range).
docCommentAbove
public · function · Source (opens in a new tab)
std::string sun::parsing::docCommentAbove(const std::string &source, int line)Returns the documentation comment preceding a source line.
encodeUtf8
public · function · Source (opens in a new tab)
int sun::parsing::encodeUtf8(uint32_t scalar, char *out)Encode scalar as UTF-8 into out (at least 4 bytes).
Returns the number of bytes written.
formatProgram
public · function · Source (opens in a new tab)
std::string sun::parsing::formatProgram(const sun::ast::BlockExprAST &program, const std::map< int, sun::parsing::Comment > &comments, const std::string &source)Format an already-parsed lossless (pre-lowering) program.
Formats a parsed program with its original comments and source context.
comments must come from the same parse (Parser::getComments()) and source must be the exact text that was parsed — literals and types are sliced from it.
Related: sun::ast::BlockExprAST, sun::parsing::Comment, Parser::getComments()
formatSource
public · function · Source (opens in a new tab)
std::string sun::parsing::formatSource(const std::string &source, const std::string &filePath="<fmt>")Parse source and reprint it in the canonical style (2-space indent, K&R braces, comments preserved).
Parses and formats Sun source text for the supplied file.
Throws SunError on parse failure; the input is never partially formatted.
Related: SunError
getKeywordSpelling
public · function · Source (opens in a new tab)
std::optional< std::string_view > sun::parsing::getKeywordSpelling(TokenKind kind)The spelling of a keyword token (a token whose regex is a bare word, so it would otherwise lex as an identifier); nullopt for any other kind.
Derived from the regex table so a new keyword is covered without a second list.
Related: TokenKind
getTokenInfo
public · function · Source (opens in a new tab)
const std::map< TokenKind, TokenInfo > & sun::parsing::getTokenInfo()Returns the token category information.
hexByte
public · function · Source (opens in a new tab)
std::optional< char > sun::parsing::hexByte(std::string_view digits)Decode the first two hex digits as a byte, or reject missing or invalid digits.
hexDigit
public · function · Source (opens in a new tab)
int sun::parsing::hexDigit(char c)Value of one hex digit, or -1.
isAssignmentOp
public · function · static · Source (opens in a new tab)
static bool sun::parsing::isAssignmentOp(TokenKind kind)True for '=' and every compound-assignment operator.
Related: TokenKind
isKeyword
public · function · Source (opens in a new tab)
bool sun::parsing::isKeyword(TokenKind kind)Reports whether the token category is a language keyword.
Related: TokenKind
isPublicableStatementStart
public · function · static · Source (opens in a new tab)
static bool sun::parsing::isPublicableStatementStart(TokenKind kind)Statement kinds that may carry a public modifier at item level.
Related: TokenKind
isScalarValue
public · function · Source (opens in a new tab)
bool sun::parsing::isScalarValue(uint32_t v)A Unicode scalar value is anything up to kMaxScalar that is not a surrogate.
isSurrogate
public · function · Source (opens in a new tab)
bool sun::parsing::isSurrogate(uint32_t v)UTF-16 surrogates are not scalar values and may not appear on their own.
normalizeBody
public · function · static · Source (opens in a new tab)
static void sun::parsing::normalizeBody(std::unique_ptr< ExprAST > &slot, bool isIfBody)Normalize an if/loop body block to the shape the core pipeline expects: empty block -> synthetic literal, single-statement block -> the statement.
The parser keeps bodies as blocks for losslessness.
Related: ExprAST
putchard
public · function · static · Source (opens in a new tab)
static double sun::parsing::putchard(double X)The actual C function that prints a character.
simple
public · function · Source (opens in a new tab)
std::optional< char > sun::parsing::simple(char c)Handles newline, tab, carriage return, backslash, and null escapes.
Returns the character the escape stands for, or nullopt if c does not name one of them (the caller decides what to do with quotes and hexadecimal or Unicode escapes).
utf8SequenceLength
public · function · Source (opens in a new tab)
int sun::parsing::utf8SequenceLength(unsigned char lead)Number of bytes in the UTF-8 sequence that starts with lead, or 0 if lead is not a valid leading byte.
Variables
kMaxScalar
public · variable · Source (opens in a new tab)
uint32_t sun::parsing::kMaxScalar = 0x10FFFFNo documentation comment.
tokenRegexes
public · variable · static · Source (opens in a new tab)
const std::map<TokenKind, std::string> sun::parsing::tokenRegexesNo documentation comment.
Related: TokenKind
Enums
TokenKind
public · enum · Source (opens in a new tab)
enum class sun::parsing::TokenKindIdentifies the lexical categories recognized by the Sun parser.
TOK_EOF
TOK_EOF
No documentation comment.
COMMENT
COMMENT
No documentation comment.
BLOCK_COMMENT
BLOCK_COMMENT
No documentation comment.
DECLARE
DECLARE
No documentation comment.
DEF
DEF
No documentation comment.
EXTERN
EXTERN
No documentation comment.
VAR
VAR
No documentation comment.
MANIFEST
MANIFEST
No documentation comment.
MODULE
MODULE
No documentation comment.
USING
USING
No documentation comment.
CLASS
CLASS
No documentation comment.
PARTIAL
PARTIAL
No documentation comment.
PUBLIC
PUBLIC
No documentation comment.
PACKED_CLASS
PACKED_CLASS
No documentation comment.
INTERFACE
INTERFACE
No documentation comment.
ENUM
ENUM
No documentation comment.
EXTENDS
EXTENDS
No documentation comment.
IMPLEMENTS
IMPLEMENTS
No documentation comment.
THIS
THIS
No documentation comment.
NULL_LITERAL
NULL_LITERAL
No documentation comment.
TRUE_LITERAL
TRUE_LITERAL
No documentation comment.
FALSE_LITERAL
FALSE_LITERAL
No documentation comment.
IF
IF
No documentation comment.
MATCH
MATCH
No documentation comment.
ELSE
ELSE
No documentation comment.
FOR
FOR
No documentation comment.
WHILE
WHILE
No documentation comment.
BREAK
BREAK
No documentation comment.
CONTINUE
CONTINUE
No documentation comment.
RETURN
RETURN
No documentation comment.
FUNCTION
FUNCTION
No documentation comment.
TEST_FUNCTION
TEST_FUNCTION
No documentation comment.
TRY
TRY
No documentation comment.
CATCH
CATCH
No documentation comment.
THROW
THROW
No documentation comment.
THROWS
THROWS
No documentation comment.
UNSAFE
UNSAFE
No documentation comment.
STATIC_PTR
STATIC_PTR
No documentation comment.
PTR
PTR
No documentation comment.
RAW_PTR
RAW_PTR
No documentation comment.
REF
REF
No documentation comment.
CONST
CONST
No documentation comment.
ARRAY
ARRAY
No documentation comment.
ARROW
ARROW
No documentation comment.
FAT_ARROW
FAT_ARROW
No documentation comment.
UNDERSCORE
UNDERSCORE
No documentation comment.
TYPE_I8
TYPE_I8
No documentation comment.
TYPE_I16
TYPE_I16
No documentation comment.
TYPE_I32
TYPE_I32
No documentation comment.
TYPE_I64
TYPE_I64
No documentation comment.
TYPE_U8
TYPE_U8
No documentation comment.
TYPE_U16
TYPE_U16
No documentation comment.
TYPE_U32
TYPE_U32
No documentation comment.
TYPE_U64
TYPE_U64
No documentation comment.
TYPE_F32
TYPE_F32
No documentation comment.
TYPE_F64
TYPE_F64
No documentation comment.
TYPE_BOOL
TYPE_BOOL
No documentation comment.
TYPE_VOID
TYPE_VOID
No documentation comment.
TYPE_CHAR
TYPE_CHAR
No documentation comment.
STRING
STRING
No documentation comment.
CHAR_LITERAL
CHAR_LITERAL
No documentation comment.
BYTE_LITERAL
BYTE_LITERAL
No documentation comment.
TEMPLATE_STRING
TEMPLATE_STRING
No documentation comment.
BRACE_OPEN
BRACE_OPEN
No documentation comment.
BRACE_CLOSE
BRACE_CLOSE
No documentation comment.
BRACKET_OPEN
BRACKET_OPEN
No documentation comment.
BRACKET_CLOSE
BRACKET_CLOSE
No documentation comment.
PLUS
PLUS
No documentation comment.
MINUS
MINUS
No documentation comment.
STAR
STAR
No documentation comment.
SLASH
SLASH
No documentation comment.
PERCENT
PERCENT
No documentation comment.
AMPERSAND
AMPERSAND
No documentation comment.
PIPE
PIPE
No documentation comment.
CARET
CARET
No documentation comment.
LEFT_SHIFT
LEFT_SHIFT
No documentation comment.
RIGHT_SHIFT
RIGHT_SHIFT
No documentation comment.
TILDE
TILDE
No documentation comment.
PLUS_ASSIGN
PLUS_ASSIGN
No documentation comment.
MINUS_ASSIGN
MINUS_ASSIGN
No documentation comment.
STAR_ASSIGN
STAR_ASSIGN
No documentation comment.
SLASH_ASSIGN
SLASH_ASSIGN
No documentation comment.
PERCENT_ASSIGN
PERCENT_ASSIGN
No documentation comment.
AMP_ASSIGN
AMP_ASSIGN
No documentation comment.
PIPE_ASSIGN
PIPE_ASSIGN
No documentation comment.
CARET_ASSIGN
CARET_ASSIGN
No documentation comment.
LEFT_SHIFT_ASSIGN
LEFT_SHIFT_ASSIGN
No documentation comment.
RIGHT_SHIFT_ASSIGN
RIGHT_SHIFT_ASSIGN
No documentation comment.
AND
AND
No documentation comment.
OR
OR
No documentation comment.
NOT
NOT
No documentation comment.
LESS
LESS
No documentation comment.
LESS_EQUAL
LESS_EQUAL
No documentation comment.
GREATER
GREATER
No documentation comment.
GREATER_EQUAL
GREATER_EQUAL
No documentation comment.
EQUAL
EQUAL
No documentation comment.
EQUAL_EQUAL
EQUAL_EQUAL
No documentation comment.
NOT_EQUAL
NOT_EQUAL
No documentation comment.
PAREN_OPEN
PAREN_OPEN
No documentation comment.
PAREN_CLOSE
PAREN_CLOSE
No documentation comment.
COMMA
COMMA
No documentation comment.
SEMI_COLON
SEMI_COLON
No documentation comment.
DOUBLE_COLON
DOUBLE_COLON
No documentation comment.
COLON
COLON
No documentation comment.
QUESTION
QUESTION
No documentation comment.
ELLIPSIS
ELLIPSIS
No documentation comment.
DOT
DOT
No documentation comment.
INTEGER
INTEGER
No documentation comment.
FLOAT
FLOAT
No documentation comment.
INTRINSIC_IDENTIFIER
INTRINSIC_IDENTIFIER
No documentation comment.
IDENTIFIER
IDENTIFIER
No documentation comment.
TYPED_FLOAT
TYPED_FLOAT
No documentation comment.
TYPED_INTEGER
TYPED_INTEGER
No documentation comment.
LIFETIME
LIFETIME
No documentation comment.
UNKNOWN
UNKNOWN
No documentation comment.
COUNT
COUNT
No documentation comment.
Type Aliases
Segment
public · typedef · Source (opens in a new tab)
using sun::parsing::Segment = sun::ast::InterpolatedStringAST::SegmentA literal or embedded-expression segment of an interpolated string.