Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .smalltalk.ston
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SmalltalkCISpec {
#testing : {
#coverage : {
#packages : [ 'FAST-Core-Model', 'FAST-Core-Model-Extension', 'FAST-Core-Tools', 'FAST-Core-Visitor' ],
#packages : [ 'FAST-Core-Model', 'FAST-Core-Model-Extension', 'FAST-Core-Tools', 'FAST-Core-Visitor', 'FAST-Importer' ],
#format : #lcov
}
},
Expand Down
45 changes: 45 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# AGENTS.md

FAST is the generic Famix AST metamodel shared by language-specific FAST projects. This repository is Pharo/Smalltalk source in Tonel format; there is no build, lint, or typecheck step — code is loaded into a Pharo/Moose image and verified with tests.

## Setup and testing

- Load the baseline from `src`:
```st
Metacello new
baseline: 'FAST';
repository: 'github://moosetechnology/FAST:v3/src';
load: 'core' "or 'All' / 'tools'"
```
- Groups are defined in `src/BaselineOfFAST/BaselineOfFAST.class.st`: `core` = model, model extensions, generator, visitor; `tools` = tools, tool tests, `FAST-Importer` and its tests; `All` = `core` + `tools` + deprecated code. Loading with no group loads all packages (there is no `default` group).
- CI runs `smalltalkci -s Moose64-12` and `-s Moose64-13` using `.smalltalk.ston`; that config loads `core` + `tools` and runs tests with lcov coverage. The README's Moose 11/12 badge is stale.
- Single test in an image: `FASTTEntityTest buildSuite run`, or `(FASTTEntityTest selector: #testBoundaryLinesAreInclusive) run`.
- The baseline loads Famix from `development` only when the image lacks `BaselineOfFamix`, and postLoad-patches Moose 12 via `ensureMoose12Compatibility`; keep that patch while Moose 12 remains in CI.
- If testing through the Pharo MCP, `image_evaluate` turns warnings (including deprecations) into errors and can interrupt the image. Avoid deprecated APIs there.

## Generated code — do not hand-edit

- `src/FAST-Core-Model` is generated by `FASTMetamodelGenerator` (FamixNG/Fame). Change the generator, run `FASTMetamodelGenerator generate`, then commit the regenerated source. Methods marked `<generated>` are overwritten.
- `FASTCoreVisitor` is generated visitor plumbing. Use `FASTVisitorCodeGenerator` (and `FASTCopyVisitorCodeGenerator` for copy visitors) instead of writing visit/accept methods by hand.
- Hand-written model behavior belongs in `FAST-Core-Model-Extension` or in package extensions such as `FAST-Core-Tools/FASTTEntity.extension.st`.
- `FASTTestMetamodelGenerator class>>initialize` generates `FAST-Core-Test-Model` when its package loads; that package is intentionally not committed. Tests such as `FASTTEntityTest` use `FASTTestClass`/`FASTTestMethod`, so load `FAST-Core-Tests-Generator` before running them.

## Importers

- `FASTTImporter` is a trait used on the instance side and on the class side of each importer (`MyImporter uses: FASTTImporter` and `MyImporter class uses: FASTTImporter classTrait`). Its class side creates an importer and delegates to the instance side, so a caller can use `MyImporter parseFile: file` or `MyImporter new parseFile: file`.
- A language importer implements `parse:into:` on its instance side, answering the FAST model after importing the source into the model given (a `nil` model means a new one), and may override `extensions` on its class side (the file extensions without the dot, none by default).
- `FASTAgnosticImporter` imports a file without knowing its importer. Its behaviour is on the instance side, and its class side has the helpers `importerFor:`, `parseFile:` and `parseFile:into:`. The importers are the classes that use `FASTTImporter classTrait` and their concrete subclasses (`isAbstract` answers false by default). The importer of a file is the one whose `extensions` include the file's extension. Several importers for the same extension raise `FASTAmbiguousImporterError`; no importer for the extension raises an error.
- When no loaded importer handles a file, `loadMissingImporterFor:` loads the project of a working description (`isWorking`) that handles the extension and is not loaded yet. Interactively, it first asks the user with `SpConfirmDialog`; otherwise it loads the project. `loadMissingImporterFor:interactive:` takes the decision as a parameter.
- `FASTImporterDescription` is the hierarchy of the projects that provide importers: repository, branch, baseline, group and extensions, with `loadProject` (network-heavy) and `loadAll`. A description is loaded (`isProjectLoaded`) when an importer for one of its extensions is present. A description with `isWorking` false (FAST-Pharo and FAST-Fortran, which do not work with the current FAST version) is listed but not loaded by `loadAll`.
- The importers of the trait are found by their users: there is no pragma to add. A new importer only has to use the trait on both sides.
- Language-specific importers and their ASTs live in separate repositories (FAST-JAVA, FAST-Python, ...), and depend on FAST and on Pharo-Tree-Sitter for the tree-sitter ones. The trait is in `FAST-Importer` on the `importer` branch until it is merged into `v3`.

## Layout and boundaries

- `FAST-Core-Model`: generic `FASTT*` traits and `FAST*Entity` classes, plus `FASTModel`.
- `FAST-Core-Tools`: handwritten CFG, SSA, local resolver, validators/comparators and visitor code generators.
- `FAST-Core-Deprecated`: legacy code, excluded from `core`.
- `FAST-Importer` (in the `tools` group): the importer API, the importer descriptions and the agnostic importer. It contains no parsers. Details are in the Importers section below.
- Language-specific ASTs and importers live in separate repositories (FAST-JAVA, FAST-Pharo, FAST-Python, ...). Add only generic nodes/relations here.
- Keep Tonel format (`src/.properties` is `#format : #tonel`); do not convert the repository to filetree.
- Tests are `TestCase` subclasses named `*Test`.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ Metacello new
load
```

## Importers

FAST imports source files into models with importers, one per language. Quickstart:

```st
"One file, in a new model"
FASTAgnosticImporter parseFile: 'src/Main.java' asFileReference.

"A folder: one model per language, the files without importer under nil"
FASTAgnosticImporter parseFolder: 'src' asFileReference.
```

The full guide, with the options for missing importers and how to write an importer, is in [resources/doc/importers.md](resources/doc/importers.md).

## FAST Meta-Model

![FAST Meta-model](https://raw.githubusercontent.com/moosetechnology/FAST/v2-doc/fast-core.png)
Expand Down
102 changes: 102 additions & 0 deletions resources/doc/importers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Importers

FAST imports source files into FAST models with importers: one importer per language, provided by a language project (FAST-JAVA, FAST-Python, ...).

You can import in two ways.

- **Use the language importer directly** when you know the language of your files. Each language importer uses the same API, so you call it on its class, for example `FASTJavaSmaCCImporter parseFile: 'src/Main.java' asFileReference`. This is the most direct choice, and it never loads a project.
- **Use `FASTAgnosticImporter`** when you have files of several languages, or do not want to choose the importer. It chooses the importer of each file from its extension, and it can load the project of a language that is not in the image yet. Most of this guide describes this way.

## Import files

The most common calls are on the class side of `FASTAgnosticImporter`.

Import one file in a new model:

```st
FASTAgnosticImporter parseFile: 'src/Main.java' asFileReference.
```

Add a file to a model you already have, for example to import several files in one model:

```st
| model |
model := FASTAgnosticImporter parseFile: 'src/Main.java' asFileReference.
FASTAgnosticImporter parseFile: 'src/Other.java' asFileReference into: model.
```

Ask which importer handles a file, without importing it:

```st
FASTAgnosticImporter importerFor: 'src/Main.java' asFileReference.
```

Import a folder, its subfolders included, or a list of files:

```st
| result |
result := FASTAgnosticImporter parseFolder: 'src' asFileReference.
result := FASTAgnosticImporter parseFiles: { 'A.java' asFileReference. 'style.css' asFileReference }.
```

The files are grouped by importer, and each importer fills one model, so each language has one model. The answer is a dictionary from each model to the files that filled it. The files that no importer handles are answered under `nil`:

```st
result keysAndValuesDo: [ :model :files |
Transcript show: files size printString; cr ].
result at: nil.
```

## Options

The options are on the instance side. Create an instance, give it a strategy, then use the same messages:

```st
FASTAgnosticImporter new
missingImporterStrategy: FASTAlwaysLoadStrategy new;
parseFolder: 'src' asFileReference.
```

When no loaded importer handles the extension of a file, FAST can load the project of a description that does. The strategy decides whether that project may be loaded:

- `FASTAskOrLoadStrategy` (the default): in an interactive image the user is asked before the project is loaded, with a Spec dialog. Otherwise the project is loaded.
- `FASTAlwaysLoadStrategy`: the project is loaded without asking. Use it to bypass the dialog.
- `FASTNeverLoadStrategy`: no project is loaded. The files it would have imported are answered under `nil`.

To write your own strategy, subclass `FASTMissingImporterStrategy` and implement `allowsLoadingOf:for:`, which answers whether the project of a description may be loaded for a file.

Loading a project needs network access.

## Errors

- When several importers handle the extension of a file, `FASTAmbiguousImporterError` is signaled, both for a single file and for a batch.
- When no importer handles a file in a single-file import, an error is signaled. A batch answers such files under `nil` instead.

## Available importers

Each language project has a `FASTImporterDescription` subclass. A description tells where its project is, and which extensions it handles. `FASTImporterDescription loadAll` loads all the projects that work, and a project is loaded when an importer for one of its extensions is present.

FAST-Pharo and FAST-Fortran are listed but do not work with the current FAST version, so `loadAll` does not load them.

## Writing an importer

An importer uses the `FASTTImporter` trait on its instance side and on its class side. In the class definition, this is the `#traits` and `#classTraits` entries:

```st
Class {
#name : 'MyLanguageImporter',
#superclass : 'Object',
#traits : 'FASTTImporter',
#classTraits : 'FASTTImporter classTrait',
...
}
```

It implements two methods:

- `parse:into:` on the instance side. It imports the source into the model given, or into a new model when the model is `nil`, and answers the model.
- `extensions` on the class side, answering the file extensions without the dot. It is `#()` by default, so an importer that does not declare extensions handles no file.

The trait gives the other entry points on both sides: `parse:`, `parseFile:` and `parseFile:into:`, and `handlesFile:`, which `FASTAgnosticImporter` uses to find the importer of a file. The importers are found by the classes that use the trait, so there is nothing to register.

Importers based on Tree-Sitter inherit from `TSFASTAbstractImporter` in Pharo-Tree-Sitter. They get `parse:usingOldTree:` for incremental parsing: after `parse:`, the model can be reparsed from a new version of its source. Only the parse is incremental: the FAST model is rebuilt.
7 changes: 4 additions & 3 deletions src/BaselineOfFAST/BaselineOfFAST.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ BaselineOfFAST >> defineGroups: spec [

spec
group: 'core' with: #( 'FAST-Core-Model' 'FAST-Core-Model-Extension' 'FAST-Model-Generator' 'FAST-Core-Visitor' );
group: 'All' with: #( 'core' 'FAST-Core-Tools' 'FAST-Core-Tools-Tests' 'FAST-Core-Deprecated');
group: 'tools' with: #( 'All' );
group: 'default' with: #( 'core' )
group: 'tools' with: #( 'FAST-Core-Tools' 'FAST-Core-Tools-Tests' 'FAST-Importer' 'FAST-Importer-Tests' );
group: 'All' with: #( 'core' 'tools' 'FAST-Core-Deprecated' )
]

{ #category : 'baselines' }
Expand All @@ -59,6 +58,8 @@ BaselineOfFAST >> definePackages: spec [
package: 'FAST-Core-Tests-Generator' with: [ spec requires: #( 'FAST-Core-Model' ) ];
package: 'FAST-Core-Tools' with: [ spec requires: #( 'FAST-Core-Visitor' ) ];
package: 'FAST-Core-Tools-Tests' with: [ spec requires: #( 'FAST-Core-Tools' 'FAST-Core-Tests-Generator' ) ];
package: 'FAST-Importer';
package: 'FAST-Importer-Tests' with: [ spec requires: #( 'FAST-Importer' ) ];
package: 'FAST-Core-Deprecated'.

spec for: #NeedsMoose do: [
Expand Down
58 changes: 58 additions & 0 deletions src/FAST-Importer-Tests/FASTAgnosticImporterBatchTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"
I test the batch import of FASTAgnosticImporter: files and folders become one model per language, and the files without importer are answered under nil.
"
Class {
#name : 'FASTAgnosticImporterBatchTest',
#superclass : 'TestCase',
#category : 'FAST-Importer-Tests-Tests',
#package : 'FAST-Importer-Tests',
#tag : 'Tests'
}

{ #category : 'tests' }
FASTAgnosticImporterBatchTest >> testNeverLoadLeavesTheMissingImporterUnloaded [

| file result loadsBefore |
file := FileSystem memory / 'data.stubext'.
file writeStreamDo: [ :stream | stream nextPutAll: 'data' ].
loadsBefore := FASTLoadableStubDescription loadCount.
result := (FASTAgnosticImporter new
missingImporterStrategy: FASTNeverLoadStrategy new;
yourself) parseFiles: { file }.
self assert: (result at: nil) asArray equals: { file }.
self assert: FASTLoadableStubDescription loadCount equals: loadsBefore
]

{ #category : 'tests' }
FASTAgnosticImporterBatchTest >> testParseFilesBuildsOneModelPerLanguage [

| fileSystem result loneModel |
fileSystem := FileSystem memory.
fileSystem / 'A.lone' writeStreamDo: [ :stream | stream nextPutAll: 'first' ].
fileSystem / 'B.lone' writeStreamDo: [ :stream | stream nextPutAll: 'second' ].
fileSystem / 'notes.txt' writeStreamDo: [ :stream | stream nextPutAll: 'not code' ].
result := FASTAgnosticImporter new parseFiles: {
(fileSystem / 'A.lone').
(fileSystem / 'B.lone').
(fileSystem / 'notes.txt') }.
self assert: result size equals: 2.
self assert: (result at: nil) equals: (OrderedCollection with: fileSystem / 'notes.txt').
loneModel := result keys detect: [ :model | model isNotNil ].
self assert: loneModel asArray equals: { 'first'. 'second' }.
self assert: (result at: loneModel) asArray equals: {
(fileSystem / 'A.lone').
(fileSystem / 'B.lone') }
]

{ #category : 'tests' }
FASTAgnosticImporterBatchTest >> testParseFolderImportsTheFilesOfTheFolder [

| fileSystem result |
fileSystem := FileSystem memory.
(fileSystem / 'src') ensureCreateDirectory.
fileSystem / 'src' / 'A.lone' writeStreamDo: [ :stream | stream nextPutAll: 'first' ].
fileSystem / 'src' / 'B.lone' writeStreamDo: [ :stream | stream nextPutAll: 'second' ].
result := FASTAgnosticImporter parseFolder: fileSystem / 'src'.
self assert: result size equals: 1.
self assert: result keys anyOne size equals: 2
]
34 changes: 34 additions & 0 deletions src/FAST-Importer-Tests/FASTAgnosticImporterTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"
I test that FASTAgnosticImporter chooses the importer of a file by its extension, refuses files that no importer handles and reports importers that are ambiguous.
"
Class {
#name : 'FASTAgnosticImporterTest',
#superclass : 'TestCase',
#category : 'FAST-Importer-Tests-Tests',
#package : 'FAST-Importer-Tests',
#tag : 'Tests'
}

{ #category : 'tests' }
FASTAgnosticImporterTest >> testDoesNotLoadAProjectForAnUnknownExtension [

self deny: (FASTAgnosticImporter new loadMissingImporterFor: 'notes.unknownext' asFileReference)
]

{ #category : 'tests' }
FASTAgnosticImporterTest >> testFindsTheImporterOfAFileByItsExtension [

self assert: (FASTAgnosticImporter importerFor: 'Main.lone' asFileReference) identicalTo: FASTExampleLoneImporter
]

{ #category : 'tests' }
FASTAgnosticImporterTest >> testRefusesAFileNoImporterHandles [

self should: [ FASTAgnosticImporter importerFor: 'notes.txt' asFileReference ] raise: Error
]

{ #category : 'tests' }
FASTAgnosticImporterTest >> testSeveralImportersForAnExtensionAreAmbiguous [

self should: [ FASTAgnosticImporter importerFor: 'file.ex' asFileReference ] raise: FASTAmbiguousImporterError
]
27 changes: 27 additions & 0 deletions src/FAST-Importer-Tests/FASTExampleImporter.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"
I am a minimal importer used to test the importer API trait. My parse:into: answers the source and the model it received.
"
Class {
#name : 'FASTExampleImporter',
#superclass : 'Object',
#traits : 'FASTTImporter',
#classTraits : 'FASTTImporter classTrait',
#category : 'FAST-Importer-Tests-Stubs',
#package : 'FAST-Importer-Tests',
#tag : 'Stubs'
}

{ #category : 'accessing' }
FASTExampleImporter class >> extensions [
"File extensions without the dot that the importer handles"

^ #( 'ex' )
]

{ #category : 'importing' }
FASTExampleImporter >> parse: aString into: aModel [

^ {
aString.
aModel }
]
27 changes: 27 additions & 0 deletions src/FAST-Importer-Tests/FASTExampleLoneImporter.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"
I am an importer for the extension lone, used to test FASTAgnosticImporter without any language project. My model is the collection of the sources I imported.
"
Class {
#name : 'FASTExampleLoneImporter',
#superclass : 'FASTExampleImporter',
#category : 'FAST-Importer-Tests-Stubs',
#package : 'FAST-Importer-Tests',
#tag : 'Stubs'
}

{ #category : 'accessing' }
FASTExampleLoneImporter class >> extensions [
"File extensions without the dot that the importer handles"

^ #( 'lone' )
]

{ #category : 'importing' }
FASTExampleLoneImporter >> parse: aString into: aModel [
"Import the source in the collection given, or in a new collection, and answer it"

| model |
model := aModel ifNil: [ OrderedCollection new ].
model add: aString.
^ model
]
10 changes: 10 additions & 0 deletions src/FAST-Importer-Tests/FASTExampleOtherImporter.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"
I am a second importer handling the same extension as FASTExampleImporter, to test that FASTAgnosticImporter reports the ambiguity.
"
Class {
#name : 'FASTExampleOtherImporter',
#superclass : 'FASTExampleImporter',
#category : 'FAST-Importer-Tests-Stubs',
#package : 'FAST-Importer-Tests',
#tag : 'Stubs'
}
17 changes: 17 additions & 0 deletions src/FAST-Importer-Tests/FASTImporterDescriptionTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"
I test that a description tells whether its project is loaded from the presence of its importer class.
"
Class {
#name : 'FASTImporterDescriptionTest',
#superclass : 'TestCase',
#category : 'FAST-Importer-Tests-Tests',
#package : 'FAST-Importer-Tests',
#tag : 'Tests'
}

{ #category : 'tests' }
FASTImporterDescriptionTest >> testIsProjectLoadedChecksTheImportersOfTheExtensions [

self assert: FASTLoneImporterDescription isProjectLoaded.
self deny: FASTMissingImporterDescription isProjectLoaded
]
Loading
Loading