Skip to content

Commit

Permalink
Add Pegen grammar for reading Python3 grammar.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 committed Sep 16, 2023
1 parent 9b7ae73 commit 6aa587d
Show file tree
Hide file tree
Showing 11 changed files with 3,575 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pegen/CSharp/Adaptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using System;
using System.IO;
using System.Reflection;

public abstract class Adaptor : Lexer
{
readonly ICharStream stream;
readonly FieldInfo tokenInput = typeof(CommonToken).GetField("_type", BindingFlags.NonPublic | BindingFlags.Instance);
protected Adaptor(ICharStream input)
: base(input, Console.Out, Console.Error)
{
stream = input;
}

protected Adaptor(ICharStream input, TextWriter output, TextWriter errorOutput)
: base(input, output, errorOutput)
{
stream = input;
}

private int CurrentRuleType { get; set; } = TokenConstants.InvalidType;

public bool AtEnd()
{
return CurrentMode == pegen_v3_10Lexer.ACTION_MODE;
}

protected void handleEndAction()
{
int oldMode = CurrentMode;
int newMode = PopMode();
bool isActionWithinAction = ModeStack.Count > 0
&& newMode == pegen_v3_10Lexer.ACTION_MODE
&& oldMode == newMode;

if (isActionWithinAction)
{
CurrentRuleType = (pegen_v3_10Lexer.ACTION);
}
}
}
31 changes: 31 additions & 0 deletions pegen/Java/Adaptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Token;

public abstract class Adaptor extends Lexer {

protected Adaptor(CharStream input) {
super(input);
}

private int CurrentRuleType = Token.INVALID_TYPE;

public boolean AtEnd()
{
return this._mode == pegen_v3_10Lexer.ACTION_MODE;
}

protected void handleEndAction()
{
int oldMode = this._mode;
int newMode = this.popMode();
boolean isActionWithinAction = !this._modeStack.isEmpty()
&& newMode == pegen_v3_10Lexer.ACTION_MODE
&& oldMode == newMode;

if (isActionWithinAction)
{
CurrentRuleType = (pegen_v3_10Lexer.ACTION);
}
}
}
4 changes: 4 additions & 0 deletions pegen/desc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<desc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../_scripts/desc.xsd">
<targets>CSharp;Java</targets>
</desc>
Loading

0 comments on commit 6aa587d

Please sign in to comment.