Skip to content
Snippets Groups Projects
Commit 32aa3263 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Parse syntaxt

parent aed1b041
Branches master
No related tags found
No related merge requests found
...@@ -3,11 +3,17 @@ ...@@ -3,11 +3,17 @@
archiveVersion = 1; archiveVersion = 1;
classes = { classes = {
}; };
objectVersion = 50; objectVersion = 52;
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
F6752A4E2683522200CC9A19 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6752A4D2683522200CC9A19 /* main.swift */; }; F6752A4E2683522200CC9A19 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6752A4D2683522200CC9A19 /* main.swift */; };
F6752A592683534800CC9A19 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = F6752A582683534800CC9A19 /* ArgumentParser */; };
F6752A5C268354B900CC9A19 /* PathKit in Frameworks */ = {isa = PBXBuildFile; productRef = F6752A5B268354B900CC9A19 /* PathKit */; };
F6752A61268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */; };
F6752A62268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
F6752A662683599500CC9A19 /* SwiftSyntax in Frameworks */ = {isa = PBXBuildFile; productRef = F6752A652683599500CC9A19 /* SwiftSyntax */; };
F6752A6826835A2F00CC9A19 /* DeclarationCollector.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6752A6726835A2F00CC9A19 /* DeclarationCollector.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */ /* Begin PBXCopyFilesBuildPhase section */
...@@ -20,11 +26,24 @@ ...@@ -20,11 +26,24 @@
); );
runOnlyForDeploymentPostprocessing = 1; runOnlyForDeploymentPostprocessing = 1;
}; };
F6752A63268357D900CC9A19 /* Embed Libraries */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
F6752A62268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Embed Libraries */,
);
name = "Embed Libraries";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */ /* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
F6752A4A2683522200CC9A19 /* SwiftMetaModelGenerator */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SwiftMetaModelGenerator; sourceTree = BUILT_PRODUCTS_DIR; }; F6752A4A2683522200CC9A19 /* SwiftMetaModelGenerator */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = SwiftMetaModelGenerator; sourceTree = BUILT_PRODUCTS_DIR; };
F6752A4D2683522200CC9A19 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; }; F6752A4D2683522200CC9A19 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = lib_InternalSwiftSyntaxParser.dylib; path = Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/lib_InternalSwiftSyntaxParser.dylib; sourceTree = DEVELOPER_DIR; };
F6752A6726835A2F00CC9A19 /* DeclarationCollector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeclarationCollector.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
...@@ -32,6 +51,10 @@ ...@@ -32,6 +51,10 @@
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
F6752A5C268354B900CC9A19 /* PathKit in Frameworks */,
F6752A61268357D900CC9A19 /* lib_InternalSwiftSyntaxParser.dylib in Frameworks */,
F6752A592683534800CC9A19 /* ArgumentParser in Frameworks */,
F6752A662683599500CC9A19 /* SwiftSyntax in Frameworks */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
...@@ -43,6 +66,7 @@ ...@@ -43,6 +66,7 @@
children = ( children = (
F6752A4C2683522200CC9A19 /* SwiftMetaModelGenerator */, F6752A4C2683522200CC9A19 /* SwiftMetaModelGenerator */,
F6752A4B2683522200CC9A19 /* Products */, F6752A4B2683522200CC9A19 /* Products */,
F6752A5E268357A200CC9A19 /* Frameworks */,
); );
sourceTree = "<group>"; sourceTree = "<group>";
}; };
...@@ -57,11 +81,20 @@ ...@@ -57,11 +81,20 @@
F6752A4C2683522200CC9A19 /* SwiftMetaModelGenerator */ = { F6752A4C2683522200CC9A19 /* SwiftMetaModelGenerator */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
F6752A5D2683577D00CC9A19 /* lib_InternalSwiftSyntaxParser.dylib */,
F6752A4D2683522200CC9A19 /* main.swift */, F6752A4D2683522200CC9A19 /* main.swift */,
F6752A6726835A2F00CC9A19 /* DeclarationCollector.swift */,
); );
path = SwiftMetaModelGenerator; path = SwiftMetaModelGenerator;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
F6752A5E268357A200CC9A19 /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */ /* End PBXGroup section */
/* Begin PBXNativeTarget section */ /* Begin PBXNativeTarget section */
...@@ -72,12 +105,18 @@ ...@@ -72,12 +105,18 @@
F6752A462683522200CC9A19 /* Sources */, F6752A462683522200CC9A19 /* Sources */,
F6752A472683522200CC9A19 /* Frameworks */, F6752A472683522200CC9A19 /* Frameworks */,
F6752A482683522200CC9A19 /* CopyFiles */, F6752A482683522200CC9A19 /* CopyFiles */,
F6752A63268357D900CC9A19 /* Embed Libraries */,
); );
buildRules = ( buildRules = (
); );
dependencies = ( dependencies = (
); );
name = SwiftMetaModelGenerator; name = SwiftMetaModelGenerator;
packageProductDependencies = (
F6752A582683534800CC9A19 /* ArgumentParser */,
F6752A5B268354B900CC9A19 /* PathKit */,
F6752A652683599500CC9A19 /* SwiftSyntax */,
);
productName = SwiftMetaModelGenerator; productName = SwiftMetaModelGenerator;
productReference = F6752A4A2683522200CC9A19 /* SwiftMetaModelGenerator */; productReference = F6752A4A2683522200CC9A19 /* SwiftMetaModelGenerator */;
productType = "com.apple.product-type.tool"; productType = "com.apple.product-type.tool";
...@@ -105,6 +144,11 @@ ...@@ -105,6 +144,11 @@
Base, Base,
); );
mainGroup = F6752A412683522200CC9A19; mainGroup = F6752A412683522200CC9A19;
packageReferences = (
F6752A572683534800CC9A19 /* XCRemoteSwiftPackageReference "swift-argument-parser" */,
F6752A5A268354B900CC9A19 /* XCRemoteSwiftPackageReference "PathKit" */,
F6752A642683598C00CC9A19 /* XCRemoteSwiftPackageReference "swift-syntax" */,
);
productRefGroup = F6752A4B2683522200CC9A19 /* Products */; productRefGroup = F6752A4B2683522200CC9A19 /* Products */;
projectDirPath = ""; projectDirPath = "";
projectRoot = ""; projectRoot = "";
...@@ -119,6 +163,7 @@ ...@@ -119,6 +163,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
F6752A6826835A2F00CC9A19 /* DeclarationCollector.swift in Sources */,
F6752A4E2683522200CC9A19 /* main.swift in Sources */, F6752A4E2683522200CC9A19 /* main.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
...@@ -285,6 +330,51 @@ ...@@ -285,6 +330,51 @@
defaultConfigurationName = Release; defaultConfigurationName = Release;
}; };
/* End XCConfigurationList section */ /* End XCConfigurationList section */
/* Begin XCRemoteSwiftPackageReference section */
F6752A572683534800CC9A19 /* XCRemoteSwiftPackageReference "swift-argument-parser" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-argument-parser";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.4.3;
};
};
F6752A5A268354B900CC9A19 /* XCRemoteSwiftPackageReference "PathKit" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/kylef/PathKit.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
};
};
F6752A642683598C00CC9A19 /* XCRemoteSwiftPackageReference "swift-syntax" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-syntax.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.50400.0;
};
};
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
F6752A582683534800CC9A19 /* ArgumentParser */ = {
isa = XCSwiftPackageProductDependency;
package = F6752A572683534800CC9A19 /* XCRemoteSwiftPackageReference "swift-argument-parser" */;
productName = ArgumentParser;
};
F6752A5B268354B900CC9A19 /* PathKit */ = {
isa = XCSwiftPackageProductDependency;
package = F6752A5A268354B900CC9A19 /* XCRemoteSwiftPackageReference "PathKit" */;
productName = PathKit;
};
F6752A652683599500CC9A19 /* SwiftSyntax */ = {
isa = XCSwiftPackageProductDependency;
package = F6752A642683598C00CC9A19 /* XCRemoteSwiftPackageReference "swift-syntax" */;
productName = SwiftSyntax;
};
/* End XCSwiftPackageProductDependency section */
}; };
rootObject = F6752A422683522200CC9A19 /* Project object */; rootObject = F6752A422683522200CC9A19 /* Project object */;
} }
{
"object": {
"pins": [
{
"package": "PathKit",
"repositoryURL": "https://github.com/kylef/PathKit.git",
"state": {
"branch": null,
"revision": "73f8e9dca9b7a3078cb79128217dc8f2e585a511",
"version": "1.0.0"
}
},
{
"package": "Spectre",
"repositoryURL": "https://github.com/kylef/Spectre.git",
"state": {
"branch": null,
"revision": "f79d4ecbf8bc4e1579fbd86c3e1d652fb6876c53",
"version": "0.9.2"
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "986d191f94cec88f6350056da59c2e59e83d1229",
"version": "0.4.3"
}
},
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/apple/swift-syntax.git",
"state": {
"branch": null,
"revision": "2fff9fc25cdc059379b6bd309377cfab45d8520c",
"version": "0.50400.0"
}
}
]
},
"version": 1
}
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6752A492683522200CC9A19"
BuildableName = "SwiftMetaModelGenerator"
BlueprintName = "SwiftMetaModelGenerator"
ReferencedContainer = "container:SwiftMetaModelGenerator.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6752A492683522200CC9A19"
BuildableName = "SwiftMetaModelGenerator"
BlueprintName = "SwiftMetaModelGenerator"
ReferencedContainer = "container:SwiftMetaModelGenerator.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "--input"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "/Users/tobias/Documents/Programmieren/Projects/WolfManagement/WolfManagementMobile/WolfManagementMobile/Data/ValueObjects/Protocol/Accident/VOAnimalAccidentProtocol.swift"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "F6752A492683522200CC9A19"
BuildableName = "SwiftMetaModelGenerator"
BlueprintName = "SwiftMetaModelGenerator"
ReferencedContainer = "container:SwiftMetaModelGenerator.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
//
// DeclarationCollector.swift
// SwiftMetaModelGenerator
//
// Created by Tobias on 23.06.21.
//
import Foundation
import SwiftSyntax
struct ClassDeclaration {
let name: String
let variables: [String]
}
class DeclarationCollector: SyntaxVisitor {
private(set) var classes: [ClassDeclaration] = []
private var currentVariables: [String] = []
override func visitPost(_ node: ClassDeclSyntax) {
let name = node.identifier.text.trimmingCharacters(in: .whitespacesAndNewlines)
classes.append(ClassDeclaration(name: name, variables: currentVariables))
currentVariables = []
}
override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
for child: PatternBindingSyntax in node.bindings {
if let parent = child.context as? VariableDeclSyntax, parent.context is ClassDeclSyntax {
let name = child.pattern.description.trimmingCharacters(in: .whitespacesAndNewlines)
currentVariables.append(name)
}
}
return .skipChildren
}
}
extension SyntaxProtocol {
var context: DeclSyntaxProtocol? {
for case let node? in sequence(first: parent, next: { $0?.parent }) {
guard let declaration = node.asProtocol(DeclSyntaxProtocol.self) else { continue }
return declaration
}
return nil
}
}
...@@ -6,6 +6,43 @@ ...@@ -6,6 +6,43 @@
// //
import Foundation import Foundation
import ArgumentParser
import SwiftSyntax
import PathKit
struct SwiftMetaModelGenerator: ParsableCommand
{
@Option(name: .shortAndLong, help: "Input file or folder")
var input: String
mutating func run() throws {
let inputPath = Path(input)
if inputPath.isDirectory {
inputPath.iterateChildren().forEach( { processFile(file: $0) })
} else {
processFile(file: inputPath)
}
}
func processFile(file: Path) {
if !(file.extension?.lowercased().hasSuffix("swift") ?? false) {
return
}
do {
let content: String = try file.read()
let collector = DeclarationCollector()
let tree = try SyntaxParser.parse(source: content)
collector.walk(tree)
print(collector.classes)
} catch {
print(error)
}
}
}
SwiftMetaModelGenerator.main()
print("Hello, World!")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment