Skip to content

Change grammar #52

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
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
91 changes: 65 additions & 26 deletions src/jmh/java/com/github/_1c_syntax/bsl/parser/JMXBSLLexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,80 @@
import org.antlr.v4.runtime.Lexer;
import org.apache.commons.io.IOUtils;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.profile.StackProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.nio.Buffer;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.SampleTime)
@Warmup(iterations = 2) // число итераций для прогрева нашей функции
@Measurement(iterations = 2, batchSize = 2)
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class JMXBSLLexerTest {

@Param({"BSLLexer"})
public String lexerClassName;

private String content;
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.addProfiler(StackProfiler.class)
.addProfiler(GCProfiler.class)
.build();

public JMXBSLLexerTest() {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream("Module.bsl")) {
assert inputStream != null;
content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
new Runner(opt).run();
}
}

@Benchmark
public void testCharStream()
throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
@Benchmark
@Fork(1)
public void testLexer(Blackhole blackhole, MyState state) throws IOException {
Tokenizer tokenizer;
if (state.mode.equals("As stream")) {
tokenizer = new Tokenizer(Thread.currentThread().getContextClassLoader().getResourceAsStream(state.fileName), state.lexer);
} else {
tokenizer = new Tokenizer(state.content, state.lexer);
}
blackhole.consume(tokenizer.getTokens());
}

@Benchmark
@Fork(1)
public void testParser(Blackhole blackhole, MyState state) throws IOException {
Tokenizer tokenizer;
if (state.mode.equals("As stream")) {
tokenizer = new Tokenizer(Thread.currentThread().getContextClassLoader().getResourceAsStream(state.fileName), state.lexer);
} else {
tokenizer = new Tokenizer(state.content, state.lexer);
}
blackhole.consume(tokenizer.getAst());
}

Class<Lexer> lexerClass = (Class<Lexer>) Class.forName("com.github._1c_syntax.bsl.parser." + lexerClassName);
Lexer lexer = (Lexer) lexerClass.getDeclaredConstructors()[0].newInstance((Object) null);
@State(Scope.Thread)
public static class MyState {

Tokenizer tokenizer = new Tokenizer(content, lexer);
tokenizer.getTokens();
}
public String lexerClassName = "BSLLexer";
@Param({"As stream", "As text"})
public String mode;
public Lexer lexer;
private String fileName = "Module.bsl";
private String content;

public String getContent() {
return content;
}

@Setup
public void init() throws Exception {
Class<?> lexerClass = (Class<Lexer>) Class.forName("com.github._1c_syntax.bsl.parser." + lexerClassName);
lexer = (Lexer) lexerClass.getDeclaredConstructors()[0].newInstance((Object) null);
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
content = IOUtils.toString(new BufferedInputStream(classLoader.getResourceAsStream(fileName)), Charset.defaultCharset());
}
}
}
2 changes: 1 addition & 1 deletion src/main/antlr/BSLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ FALSE : 'ЛОЖЬ' | 'FALSE';
UNDEFINED : 'НЕОПРЕДЕЛЕНО' | 'UNDEFINED';
NULL : 'NULL';
DECIMAL: DIGIT+;
DATETIME: SQUOTE(~['\n\r])*SQUOTE?; // TODO: Честная регулярка
DATETIME: SQUOTE (DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT )(DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT)? SQUOTE?; // TODO: Честная регулярка
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Честная регулярка несколько сложнее. там допустимы сторонние символы.

https://github.com/1c-syntax/1c-syntax/blob/master/1c.YAML-tmLanguage#L696


FLOAT : DIGIT+ '.' DIGIT*;
STRING: '"' (~[\r\n"] | '""')* '"';
Expand Down
Loading