-
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 all 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,160 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| package org.eolang.lints; | ||
|
|
||
| import com.github.lombrozo.xnav.Xnav; | ||
| import com.google.common.base.Splitter; | ||
| import com.jcabi.xml.XML; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| 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 parser; | ||
|
|
||
| /** | ||
| * Default ctor. | ||
| */ | ||
| LtSyntaxVersion() { | ||
| this("0.0.0"); | ||
| } | ||
|
|
||
| /** | ||
| * Ctor. | ||
| * @param ver The parser version (must be valid SemVer). | ||
| */ | ||
| @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") | ||
| LtSyntaxVersion(final String ver) { | ||
| if (ver == null || !LtSyntaxVersion.valid(ver)) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "parser version must be valid SemVer (e.g. 1.2.3), got: %s", | ||
| Optional.ofNullable(ver) | ||
| .map(v -> String.format("\"%s\"", v)) | ||
| .orElse("null") | ||
| ) | ||
| ); | ||
| } | ||
| this.parser = ver; | ||
| } | ||
|
|
||
| @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)) { | ||
| if (this.newer(tail)) { | ||
| 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.parser | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| } else { | ||
| 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 | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| 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 ver The version to validate. | ||
| * @return True if valid. | ||
| */ | ||
| private static boolean valid(final String ver) { | ||
| return ver.matches("^\\d+\\.\\d+\\.\\d+(-[a-zA-Z0-9-]+)?$"); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the syntax version is newer than the parser version. | ||
| * @param syntax The version from +syntax meta. | ||
| * @return True if parser version is older than syntax version. | ||
| */ | ||
| private boolean newer(final String syntax) { | ||
| return Arrays.compare( | ||
| LtSyntaxVersion.parts(this.parser), | ||
| LtSyntaxVersion.parts(syntax) | ||
| ) < 0; | ||
| } | ||
|
|
||
| /** | ||
| * Parse the numeric parts of a SemVer string. | ||
| * @param ver The version string. | ||
| * @return Array of major, minor, patch. | ||
| */ | ||
| private static int[] parts(final String ver) { | ||
| final List<String> segments = Splitter.on('.').splitToList( | ||
| ver.split("-", 2)[0] | ||
| ); | ||
| return new int[]{ | ||
| Integer.parseInt(segments.get(0)), | ||
| Integer.parseInt(segments.get(1)), | ||
| Integer.parseInt(segments.get(2)), | ||
| }; | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -31,7 +31,8 @@ final class MonoLints extends IterableEnvelope<Lint<XML>> { | |
| new PkByXsl(), | ||
| List.of( | ||
| new LtAsciiOnly(), | ||
| new LtReservedName() | ||
| new LtReservedName(), | ||
| new LtSyntaxVersion() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Daniilmipt this will set the version to |
||
| ) | ||
| ) | ||
| ); | ||
|
|
||
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.
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.