diff --git a/src/faust-lib/common/application.cpp b/src/faust-lib/common/application.cpp index a3ca64b..5fc8fe7 100644 --- a/src/faust-lib/common/application.cpp +++ b/src/faust-lib/common/application.cpp @@ -4,10 +4,7 @@ std::optional appInfo; - -void faust::setApplicationInformation(faust::ApplicationInformation&& newAppInfo) { - appInfo = newAppInfo; -} +void faust::setApplicationInformation(faust::ApplicationInformation &&newAppInfo) { appInfo = newAppInfo; } faust::ApplicationInformation faust::getApplicationInformation() { if (!appInfo.has_value()) { diff --git a/src/faust-lib/common/string.h b/src/faust-lib/common/string.h index ba5ac5e..2de41eb 100644 --- a/src/faust-lib/common/string.h +++ b/src/faust-lib/common/string.h @@ -1,7 +1,7 @@ #pragma once -#include "unicode/unistr.h" #include "unicode/locid.h" +#include "unicode/unistr.h" namespace faust { diff --git a/src/faust-lib/parser/abstractSyntaxNodes.cpp b/src/faust-lib/parser/abstractSyntaxNodes.cpp index 9da51b3..3088786 100644 --- a/src/faust-lib/parser/abstractSyntaxNodes.cpp +++ b/src/faust-lib/parser/abstractSyntaxNodes.cpp @@ -5,10 +5,9 @@ #include faust::String faust::NamespaceAST::toString() const { - String outputString = - u"{" - u"\"astType\":\"Namespace\"," - u"\"name\":\""; + String outputString = u"{" + u"\"astType\":\"Namespace\"," + u"\"name\":\""; outputString.append(name); outputString.append("\",\"content\":["); @@ -16,7 +15,7 @@ faust::String faust::NamespaceAST::toString() const { for (auto it = statements.begin(); it != statements.end(); ++it) { outputString.append((*it)->toString()); if (std::next(it) != statements.end()) { - outputString.append(u","); + outputString.append(u","); } } outputString.append(u"]}"); @@ -32,8 +31,7 @@ faust::String faust::ImportAST::toString() const { } faust::String faust::VariableDeclAST::toString() const { - String outputString = - u"{\"astType\":\"Variable Declaration\",\"name\":\""; + String outputString = u"{\"astType\":\"Variable Declaration\",\"name\":\""; outputString.append(name); outputString.append(u"\",\"type\":\""); @@ -43,8 +41,7 @@ faust::String faust::VariableDeclAST::toString() const { outputString.append(u"\",\"initValue\":"); if (initializationValue.has_value()) { outputString.append((*initializationValue)->toString()); - } - else { + } else { outputString.append(u"\"__auto__\""); } outputString.append(u"}"); @@ -58,8 +55,7 @@ faust::String faust::FunctionDeclAST::toString() const { outputString.append(u"\",\"body\":"); if (body.has_value()) { outputString.append((*body)->toString()); - } - else { + } else { outputString.append(u"\"__external__\""); } outputString.append(u",\"arguments\":["); @@ -68,7 +64,7 @@ faust::String faust::FunctionDeclAST::toString() const { outputString.append((*it)->toString()); if (std::next(it) != arguments.end()) { outputString.append(","); - } + } } outputString.append("]}"); @@ -77,8 +73,7 @@ faust::String faust::FunctionDeclAST::toString() const { } faust::String faust::ExpressionBlockAST::toString() const { - String outputString = - u"{\"astType\":\"Expression Block\",\"expressions\":["; + String outputString = u"{\"astType\":\"Expression Block\",\"expressions\":["; for (auto it = statements.begin(); it != statements.end(); ++it) { outputString.append((*it)->toString()); diff --git a/src/faust-lib/parser/abstractSyntaxNodes.h b/src/faust-lib/parser/abstractSyntaxNodes.h index 0ff48b1..a7362e6 100644 --- a/src/faust-lib/parser/abstractSyntaxNodes.h +++ b/src/faust-lib/parser/abstractSyntaxNodes.h @@ -22,27 +22,28 @@ enum AbstractSyntaxType { class AbstractSyntaxNode { public: - AbstractSyntaxNode(AbstractSyntaxType type, CodePosition position) - : astType(type), position(position) {} + AbstractSyntaxNode(AbstractSyntaxType type, CodePosition position) : astType(type), position(position) {} virtual ~AbstractSyntaxNode() = default; virtual String toString() const = 0; inline AbstractSyntaxType getType() const { return astType; } inline CodePosition getPosition() const { return position; } + private: AbstractSyntaxType astType; CodePosition position; }; typedef std::unique_ptr AstNodePtr; -typedef AbstractSyntaxNode* AstNodeWeakPtr; +typedef AbstractSyntaxNode *AstNodeWeakPtr; class NamespaceAST : public AbstractSyntaxNode { public: - inline NamespaceAST(CodePosition position, const String& namespaceName, Vector> &&statements) - : AbstractSyntaxNode(AST_PROGRAM, position) - , name(namespaceName), statements(std::move(statements)) {} + inline NamespaceAST(CodePosition position, const String &namespaceName, + Vector> &&statements) + : AbstractSyntaxNode(AST_PROGRAM, position), name(namespaceName), statements(std::move(statements)) {} virtual ~NamespaceAST() = default; virtual String toString() const override; + private: String name; Vector> statements; @@ -50,42 +51,29 @@ private: class ImportAST : public AbstractSyntaxNode { public: - inline ImportAST(CodePosition position, const String &path) - : AbstractSyntaxNode(AST_IMPORT, position) - , path(path) {} + inline ImportAST(CodePosition position, const String &path) : AbstractSyntaxNode(AST_IMPORT, position), path(path) {} virtual ~ImportAST() = default; virtual String toString() const override; + private: String path; }; class VariableDeclAST : public AbstractSyntaxNode { public: - inline VariableDeclAST(CodePosition position, - const String &name, - AstNodePtr&& initValue, - const std::optional& typeName, - bool isConstant - ) - : AbstractSyntaxNode(AST_VARIABLE_DECL, position) - , name(name) - , type(typeName) - , constant(isConstant) - , initializationValue(std::move(initValue)) {} + inline VariableDeclAST(CodePosition position, const String &name, AstNodePtr &&initValue, + const std::optional &typeName, bool isConstant) + : AbstractSyntaxNode(AST_VARIABLE_DECL, position), name(name), type(typeName), constant(isConstant), + initializationValue(std::move(initValue)) {} - inline VariableDeclAST(CodePosition position, - const String &name, - const std::optional& typeName, - bool isConstant - ) - : AbstractSyntaxNode(AST_VARIABLE_DECL, position) - , name(name) - , type(typeName) - , constant(isConstant) - , initializationValue(std::nullopt) {} + inline VariableDeclAST(CodePosition position, const String &name, const std::optional &typeName, + bool isConstant) + : AbstractSyntaxNode(AST_VARIABLE_DECL, position), name(name), type(typeName), constant(isConstant), + initializationValue(std::nullopt) {} virtual ~VariableDeclAST() = default; virtual String toString() const override; + private: String name; // If type is explicitly declared store it here @@ -96,19 +84,16 @@ private: class FunctionDeclAST : public AbstractSyntaxNode { public: - inline FunctionDeclAST(CodePosition position, const String &name, Vector &&arguments, AstNodePtr&& body) - : AbstractSyntaxNode(AST_FUNCTION_DECL, position) - , name(name) - , arguments(std::move(arguments)) - , body(std::move(body)) {} + inline FunctionDeclAST(CodePosition position, const String &name, Vector &&arguments, AstNodePtr &&body) + : AbstractSyntaxNode(AST_FUNCTION_DECL, position), name(name), arguments(std::move(arguments)), + body(std::move(body)) {} inline FunctionDeclAST(CodePosition position, const String &name, Vector &&arguments) - : AbstractSyntaxNode(AST_FUNCTION_DECL, position) - , name(name) - , arguments(std::move(arguments)) {} + : AbstractSyntaxNode(AST_FUNCTION_DECL, position), name(name), arguments(std::move(arguments)) {} virtual ~FunctionDeclAST() = default; virtual String toString() const override; + private: String name; Vector arguments; @@ -117,12 +102,13 @@ private: class ExpressionBlockAST : public AbstractSyntaxNode { public: - inline ExpressionBlockAST(CodePosition position, Vector &&statements, std::optional&& finalStatement) - : AbstractSyntaxNode(AST_PROGRAM, position) - , finalStatement(std::move(finalStatement)) - , statements(std::move(statements)) {} + inline ExpressionBlockAST(CodePosition position, Vector &&statements, + std::optional &&finalStatement) + : AbstractSyntaxNode(AST_PROGRAM, position), finalStatement(std::move(finalStatement)), + statements(std::move(statements)) {} virtual ~ExpressionBlockAST() = default; virtual String toString() const override; + private: std::optional finalStatement; Vector statements; diff --git a/src/faust-lib/parser/parser.h b/src/faust-lib/parser/parser.h index fcbe429..51a5774 100644 --- a/src/faust-lib/parser/parser.h +++ b/src/faust-lib/parser/parser.h @@ -4,8 +4,8 @@ #include "faust-lib/common/core.h" -#include "faust-lib/parser/faustLexer.h" #include "faust-lib/parser/abstractSyntaxNodes.h" +#include "faust-lib/parser/faustLexer.h" #include "faust-lib/parser/lexer.h" #include #include @@ -25,24 +25,22 @@ struct ParserError { String message; }; -inline ParserError createParserError(CodePosition position, const String &message) { - return {position, message}; -} +inline ParserError createParserError(CodePosition position, const String &message) { return {position, message}; } inline ParserError createUnexpectedTokenError(CodePosition position, faust::FaustTokens &token) { UErrorCode status = U_ZERO_ERROR; icu::UnicodeString pattern = u"Unexpected token: {0}"; icu::MessageFormat msgFormat(pattern, icu::Locale::getDefault(), status); - + // Create argument with token value icu::UnicodeString tokenStr = icu::UnicodeString::fromUTF8(std::to_string((static_cast(token)))); icu::Formattable args[] = {tokenStr}; - + // Format the message icu::UnicodeString result; icu::FieldPosition fieldPos; msgFormat.format(args, 1, result, fieldPos, status); - + return {position, result}; } @@ -65,9 +63,9 @@ private: u64 position; /// Helper functions - FaustLexeme& advance(); + FaustLexeme &advance(); bool hasNext(); - FaustLexeme& peek(); + FaustLexeme &peek(); static String extractStringToken(String base); /// Parsing states @@ -82,7 +80,6 @@ private: // If the expression is not followed by a semicolon expect it to be block finalization. // Errors are also treated as final expressions. ExpressionParserResult parseExpression(); - }; } // namespace faust diff --git a/tests/parserTest.cpp b/tests/parserTest.cpp index 10cbdc7..d69fccc 100644 --- a/tests/parserTest.cpp +++ b/tests/parserTest.cpp @@ -2,34 +2,31 @@ #include "faust-lib/parser/parser.h" -const icu::UnicodeString testText = -"import \"test\"; // Import some file \n" -"/* Multiline comment \n" -"Some multiline shenanigans */ \n" -"// Now going into a method \n" -"fn test /*before parameters */(const a, mut b /* implicitly null */) { // EOL\n" -" // first line of function \n" -" const c = 0; // some comment \n" -" mut d = 0; /* some comment */ \n" -" if (a == b) { // some comment \n" -" d = 1; \n" -" } else { \n" -" d = 2; \n" -" } \n" -" while (a == b) { // some comment \n" -" d = 3; \n" -" } \n" -" const strWithoutComment = \"//Hello/* world */\"; // some comment \n" -" check(a == b); // some comment \n" -" force(a == b); // some comment \n" -" return a + b; \n" -"} \n"; +const icu::UnicodeString testText = "import \"test\"; // Import some file \n" + "/* Multiline comment \n" + "Some multiline shenanigans */ \n" + "// Now going into a method \n" + "fn test /*before parameters */(const a, mut b /* implicitly null */) { // EOL\n" + " // first line of function \n" + " const c = 0; // some comment \n" + " mut d = 0; /* some comment */ \n" + " if (a == b) { // some comment \n" + " d = 1; \n" + " } else { \n" + " d = 2; \n" + " } \n" + " while (a == b) { // some comment \n" + " d = 3; \n" + " } \n" + " const strWithoutComment = \"//Hello/* world */\"; // some comment \n" + " check(a == b); // some comment \n" + " force(a == b); // some comment \n" + " return a + b; \n" + "} \n"; -class ParserTestInterface { +class ParserTestInterface { public: - static faust::Vector getTokens(faust::Parser &parser) { - return parser.state; - } + static faust::Vector getTokens(faust::Parser &parser) { return parser.state; } }; TEST(CommentFiltering, Parser) {