Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
YannickFricke committed Dec 13, 2024
1 parent 84de014 commit 61feefc
Show file tree
Hide file tree
Showing 26 changed files with 2,154 additions and 9 deletions.
3 changes: 2 additions & 1 deletion apps/ying/bin/ying.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:ying/commands/build_command.dart';
import 'package:ying/commands/init_command.dart';
import 'package:ying_shared/cli/application.dart';
import 'package:ying_shared/logging/logger.dart';
Expand All @@ -14,7 +15,7 @@ void main(List<String> arguments) {
name: "ying",
description: "The official CLI for the Yang language",
"0.0.0",
[InitCommand()],
[InitCommand(), BuildCommand()],
);

application.run(arguments);
Expand Down
41 changes: 41 additions & 0 deletions apps/ying/lib/commands/build_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:io';

import 'package:result_type/result_type.dart';
import 'package:ying_shared/cli/command.dart';
import 'package:ying_shared/cli/command_execution_context.dart';
import 'package:ying_shared/cli/flag.dart';
import 'package:ying_shared/languages/yang/parser.dart';
import 'package:ying_shared/logging/logger.dart';
import 'package:ying_shared/source_file.dart';

class BuildCommand extends Command {
final Logger logger = Logger.withSimpleNameFromEnv("build");

BuildCommand()
: super("build", [], [], [
FileFlag("entrypoint", ["e"], true, true),
]);

@override
void execute(CommandExecutionContext commandExecutionContext) async {
final File entrypoint = commandExecutionContext.parsedFlags["entrypoint"];

if (entrypoint.existsSync() == false) {
logger.error("File ${entrypoint.absolute.path} does not exist");

return;
}

final sourceFile = await SourceFile.readFromPath(entrypoint.absolute.path);

switch (YangFrontend.parseFromSourceFile(sourceFile)) {
case Success(value: var parsedStatements):
logger.debug("Result: $parsedStatements");
break;

case Failure(failure: var reason):
logger.error("Could not parse ${sourceFile.uri}: $reason");
return;
}
}
}
2 changes: 1 addition & 1 deletion packages/ying-shared/lib/cli/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class Application {
try {
await commandToRun.execute(commandExecutionContext);
} catch (error) {
print(
logger.error(
"An unexpected error occurred while executing command '${commandToRun.name}': $error",
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class CommandExecutionContext {

@override
String toString() {
return stringifyInstance("CommandExecutionContext", {
return stringifyInstance("CommandExecutionContext", fields: {
"parsedArguments": parsedArguments,
"parsedFlags": parsedFlags,
});
Expand Down
165 changes: 165 additions & 0 deletions packages/ying-shared/lib/diagnostic.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import 'package:ying_shared/text_span.dart';

enum DiagnosticSeverity {
error(1),
warning(2),
information(3),
hint(4);

final int value;

const DiagnosticSeverity(this.value);
}

enum DiagnosticTag {
unnecessary(1),
deprecated(2);

final int value;

const DiagnosticTag(this.value);
}

class Location {
final String uri;

final TextSpan range;

const Location(this.uri, this.range);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Location &&
runtimeType == other.runtimeType &&
uri == other.uri &&
range == other.range;

@override
int get hashCode => uri.hashCode ^ range.hashCode;
}

class DiagnosticRelatedInformation {
final Location location;

final String message;

const DiagnosticRelatedInformation(this.location, this.message);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is DiagnosticRelatedInformation &&
runtimeType == other.runtimeType &&
location == other.location &&
message == other.message;

@override
int get hashCode => location.hashCode ^ message.hashCode;
}

class Diagnostic {
/// The range at which the message applies.
final TextSpan range;

/// The diagnostic's severity. To avoid interpretation mismatches when a
/// server is used with different clients it is highly recommended that
/// servers always provide a severity value. If omitted, it’s recommended
/// for the client to interpret it as an Error severity.
final DiagnosticSeverity severity;

/// The diagnostic's code, which might appear in the user interface.
final String? code;

/// A human-readable string describing the source of this
/// diagnostic, e.g. 'typescript' or 'super lint'.
final String? source;

/// The diagnostic's message.
final String message;

/// Additional metadata about the diagnostic.
final List<DiagnosticTag>? tags;

/// An array of related diagnostic information, e.g. when symbol-names within
/// a scope collide all definitions can be marked via this property.
final List<DiagnosticRelatedInformation>? relatedInformation;

final dynamic data;

const Diagnostic(
this.range,
this.message, {
this.severity = DiagnosticSeverity.error,
this.code,
this.source,
this.tags,
this.relatedInformation,
this.data,
});

Diagnostic.error(
this.range,
this.message, {
this.code,
this.source,
this.tags,
this.relatedInformation,
this.data,
}) : severity = DiagnosticSeverity.error;

Diagnostic.warning(
this.range,
this.message, {
this.code,
this.source,
this.tags,
this.relatedInformation,
this.data,
}) : severity = DiagnosticSeverity.warning;

Diagnostic.information(
this.range,
this.message, {
this.code,
this.source,
this.tags,
this.relatedInformation,
this.data,
}) : severity = DiagnosticSeverity.information;

Diagnostic.hint(
this.range,
this.message, {
this.code,
this.source,
this.tags,
this.relatedInformation,
this.data,
}) : severity = DiagnosticSeverity.hint;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Diagnostic &&
runtimeType == other.runtimeType &&
range == other.range &&
severity == other.severity &&
code == other.code &&
source == other.source &&
message == other.message &&
tags == other.tags &&
relatedInformation == other.relatedInformation &&
data == other.data;

@override
int get hashCode =>
range.hashCode ^
severity.hashCode ^
code.hashCode ^
source.hashCode ^
message.hashCode ^
tags.hashCode ^
relatedInformation.hashCode ^
data.hashCode;
}
56 changes: 56 additions & 0 deletions packages/ying-shared/lib/languages/yang/expression.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:ying_shared/located_token.dart';
import 'package:ying_shared/languages/yang/type.dart';
import 'package:ying_shared/utils/stringify.dart';

abstract class Expression {
@override
String toString() => stringifyInstance("Expression");
}

class IdentifierExpression extends Expression {
final String value;

IdentifierExpression(this.value);

@override
String toString() => stringifyInstance("IdentifierExpression", fields: {
"value": value,
});
}

abstract class Statement {
@override
String toString() => stringifyInstance("Statement");
}

class ExportStatement extends Statement {
final Statement expression;

ExportStatement(this.expression);

@override
String toString() => stringifyInstance("ExportExpression", fields: {
"expression": expression,
});

static (int, ExportStatement)? fromTokens(
List<LocatedToken> locatedTokens,
int currentPosition,
) {
return null;
}
}

class TypeStatement extends Statement {
Type identifier;

Expression typeExpression;

TypeStatement(this.identifier, this.typeExpression);

@override
String toString() => stringifyInstance("TypeStatement", fields: {
"identifier": identifier,
"typeExpression": typeExpression,
});
}
12 changes: 12 additions & 0 deletions packages/ying-shared/lib/languages/yang/identifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:ying_shared/languages/yang/type_argument.dart';

abstract class Identifier {}

class UnresolvedIdentifier extends Identifier {
final String base;
final List<TypeArgument> typeArguments;

UnresolvedIdentifier(this.base) : typeArguments = [];

UnresolvedIdentifier.withTypeArguments(this.base, this.typeArguments);
}
Loading

0 comments on commit 61feefc

Please sign in to comment.