-
Notifications
You must be signed in to change notification settings - Fork 19
Feature(#797): add +syntax in meta #798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Daniilmipt
wants to merge
10
commits into
objectionary:master
Choose a base branch
from
Daniilmipt:feature(#797)
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ef423dd
#797: add +syntax in meta
f518d30
#797: add checking parser version
11f6230
remove copy file
26fb994
fix linter
a8fe095
fix parserVersion checking
8d65972
fix linter
d40a44f
again try fix linter
03a54ac
finally fix
5b8eb1b
fix linter and refactor
7e00764
Add syntax version to MonoLints
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| package org.eolang.lints; | ||
|
|
||
| import com.github.lombrozo.xnav.Xnav; | ||
| import com.jcabi.xml.XML; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.cactoos.io.ResourceOf; | ||
| import org.cactoos.text.IoCheckedText; | ||
| import org.cactoos.text.TextOf; | ||
| import org.eolang.parser.OnDefault; | ||
|
|
||
| /** | ||
| * Checks that the version specified in the +syntax meta is not | ||
| * newer than the current parser version. | ||
| * | ||
| * <p>If the +syntax meta specifies a version higher than the parser's | ||
| * version, it means the code requires a newer parser, and this | ||
| * lint reports an error.</p> | ||
| * | ||
| * @since 0.1.0 | ||
| */ | ||
| final class LtSyntaxVersion implements Lint<XML> { | ||
|
|
||
| /** | ||
| * The parser version. | ||
| */ | ||
| private final String parserVersion; | ||
|
|
||
| /** | ||
| * Default ctor. | ||
| */ | ||
| LtSyntaxVersion() { | ||
| this.parserVersion = "0.0.0"; | ||
| } | ||
|
|
||
| /** | ||
| * Ctor. | ||
| * @param parserVersion The parser version. | ||
| */ | ||
| LtSyntaxVersion(final String parserVersion) { | ||
| this.parserVersion = parserVersion; | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<Defect> defects(final XML xmir) throws IOException { | ||
| final Collection<Defect> defects = new ArrayList<>(0); | ||
| final Xnav xml = new Xnav(xmir.inner()); | ||
| final List<Xnav> metas = xml.path("/object/metas/meta[head='syntax']") | ||
| .collect(Collectors.toList()); | ||
| for (final Xnav meta : metas) { | ||
| final String tail = meta.element("tail").text().orElse(""); | ||
| final String line = meta.attribute("line").text().orElse("0"); | ||
| if (!LtSyntaxVersion.valid(tail)) { | ||
| defects.add( | ||
| new Defect.Default( | ||
| this.name(), | ||
| Severity.ERROR, | ||
| new OnDefault(xmir).get(), | ||
| Integer.parseInt(line), | ||
| String.format( | ||
| "The format of the +syntax meta is wrong: %s (SemVer expected instead)", | ||
| tail | ||
| ) | ||
| ) | ||
| ); | ||
| continue; | ||
| } | ||
| if (this.compareVersions(tail) < 0) { | ||
| defects.add( | ||
| new Defect.Default( | ||
| this.name(), | ||
| Severity.ERROR, | ||
| new OnDefault(xmir).get(), | ||
| Integer.parseInt(line), | ||
| String.format( | ||
| "The +syntax meta requires version %s, but the current parser version is %s (older)", | ||
| tail, | ||
| this.parserVersion | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| return defects; | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return "syntax-version-mismatch"; | ||
| } | ||
|
|
||
| @Override | ||
| public String motive() throws IOException { | ||
| return new IoCheckedText( | ||
| new TextOf( | ||
| new ResourceOf( | ||
| "org/eolang/motives/metas/syntax-version-mismatch.md" | ||
| ) | ||
| ) | ||
| ).asString(); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the version string is a valid SemVer. | ||
| * @param version The version to validate. | ||
| * @return True if valid. | ||
| */ | ||
| private static boolean valid(final String version) { | ||
| return version.matches("^\\d+\\.\\d+\\.\\d+(-[a-zA-Z0-9-]+)?$"); | ||
| } | ||
|
Daniilmipt marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Compare parser version to the given syntax version. | ||
| * @param syntaxVersion The version from +syntax meta. | ||
| * @return -1 if parser is older, 0 if equal, 1 if parser is newer. | ||
| */ | ||
| private int compareVersions(final String syntaxVersion) { | ||
| final int[] syntaxVer = LtSyntaxVersion.parts(syntaxVersion); | ||
| final int[] parserVer = LtSyntaxVersion.parts(this.parserVersion); | ||
| for (int i = 0; i < syntaxVer.length; i++) { | ||
| if (parserVer[i] < syntaxVer[i]) { | ||
| return -1; | ||
| } | ||
| if (parserVer[i] > syntaxVer[i]) { | ||
| return 1; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
Daniilmipt marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Parse the numeric parts of a SemVer string. | ||
| * @param version The version string. | ||
| * @return Array of [major, minor, patch]. | ||
| */ | ||
| private static int[] parts(final String version) { | ||
| final String[] segments = version.split("-", 2)[0].split("\\."); | ||
| return new int[] { | ||
| segments.length > 0 ? Integer.parseInt(segments[0]) : 0, | ||
| segments.length > 1 ? Integer.parseInt(segments[1]) : 0, | ||
| segments.length > 2 ? Integer.parseInt(segments[2]) : 0 | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| package org.eolang.lints; | ||
|
|
||
| import com.jcabi.xml.XML; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.cactoos.iterable.IterableEnvelope; | ||
| import org.cactoos.iterable.Joined; | ||
| import org.cactoos.iterable.Shuffled; | ||
| import org.cactoos.list.ListOf; | ||
|
|
||
| /** | ||
| * Mono lints. | ||
| * Mono lints represent a list of lints for single XMIR scope. This class is required | ||
| * in order to provide more fine-grained access and be reused by other classes, including | ||
| * {@link PkMono} and {@link PkWpa} via {@link MonoLintNames}, to mutually ignore other | ||
| * scope lints in both: {@link LtUnlintNonExistingDefect} and {@link LtUnlintNonExistingDefectWpa} | ||
| * without causing recursion errors. | ||
| * @since 0.0.43 | ||
| */ | ||
| final class MonoLints extends IterableEnvelope<Lint<XML>> { | ||
|
|
||
| /** | ||
| * All XML-based lints. | ||
| */ | ||
| private static final Iterable<Lint<XML>> LINTS = new Shuffled<>( | ||
| new Joined<Lint<XML>>( | ||
| new PkByXsl(), | ||
| List.of( | ||
| new LtAsciiOnly(), | ||
| new LtReservedName(), | ||
| new LtSyntaxVersion() | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| /** | ||
| * Ctor. | ||
| */ | ||
| MonoLints() { | ||
| super( | ||
| new Joined<Lint<XML>>( | ||
| MonoLints.LINTS, | ||
| List.of( | ||
| new LtIncorrectUnlint( | ||
| new ListOf<>( | ||
| new Joined<>( | ||
| MonoLints.LINTS, new WpaLints(), | ||
| new ListOf<>( | ||
| new LtUnlintNonExistingDefect( | ||
| MonoLints.LINTS, new ListOf<>(new WpaLintNames()) | ||
| ) | ||
| ) | ||
| ) | ||
| ).stream() | ||
| .map(Lint::name) | ||
| .collect(Collectors.toList()) | ||
| ) | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
Daniilmipt marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/resources/org/eolang/motives/metas/syntax-version-mismatch.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Syntax version mismatch | ||
|
|
||
| The `+syntax` meta specifies the version of the EO language that | ||
| the source code was written for. If the parser version is older | ||
| than the version specified in `+syntax`, the code may use features | ||
| that are not supported by the parser, leading to compilation errors. | ||
|
|
||
| The parser will refuse to process such files. | ||
|
|
||
| Incorrect (if parser version is 0.58.0): | ||
|
|
||
| ```eo | ||
| +syntax 0.59.0 | ||
|
|
||
| [] > foo | ||
| ``` | ||
|
|
||
| Correct (if parser version is 0.59.0 or newer): | ||
|
|
||
| ```eo | ||
| +syntax 0.59.0 | ||
|
|
||
| [] > foo | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.