Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
126 lines (111 sloc) 6.57 KB
using Nitra.Internal;
using Nitra.Internal.Recovery;
using Nitra.ProjectSystem;
using Nemerle;
using Nemerle.Assertions;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using SCG = System.Collections.Generic;
namespace Nitra
{
public class ParseSession
{
public static DeafultRecoveryTimeout : TimeSpan = TimeSpan.FromMilliseconds(200);
public this(startRuleDescriptor : StartRuleDescriptor,
compositeGrammar : CompositeGrammar = null,
completionPrefix : string = null,
completionStartPos : int = -1,
cancellationToken : CancellationToken = default(CancellationToken),
parseToEndOfString : bool = true,
recoveryTimeout : TimeSpan = default(TimeSpan),
onRecovery : Action[IParseResult] = null,
dynamicExtensions : Seq[GrammarDescriptor] = null,
statistics : StatisticsTask.Container = null,
compilerMessages : ICompilerMessages = null)
{
_compositeGrammar = compositeGrammar;
_startRuleDescriptor = startRuleDescriptor;
_completionPrefix = completionPrefix;
_completionStartPos = completionStartPos;
_cancellationToken = cancellationToken;
_parseToEndOfString = parseToEndOfString;
_recoveryTimeout = if (recoveryTimeout == default(TimeSpan)) DeafultRecoveryTimeout else recoveryTimeout;
_onRecovery = onRecovery;
DynamicExtensions = dynamicExtensions;
Statistics = statistics;
_compilerMessages = if (compilerMessages == null) CompilerMessageList() else compilerMessages;
}
public StartRuleDescriptor : StartRuleDescriptor { get { _startRuleDescriptor } set { Check(); _startRuleDescriptor = value; } }
public CompositeGrammar : CompositeGrammar
{
get
{
when (_compositeGrammar == null)
_compositeGrammar = ParserHost.Instance.DefaultCompositeGrammar.Add(this.StartRuleDescriptor.Grammar);
_compositeGrammar
}
set
{
Check();
_compositeGrammar = value;
}
}
public CompletionPrefix : string { get { _completionPrefix } set { Check(); _completionPrefix = value; } }
public CompletionStartPos : int { get { _completionStartPos } set { Check(); _completionStartPos = value; } }
public CancellationToken : CancellationToken { get { _cancellationToken } set { Check(); _cancellationToken = value; } }
public ParseToEndOfString : bool { get { _parseToEndOfString } set { Check(); _parseToEndOfString = value; } }
public RecoveryTimeout : TimeSpan { get { _recoveryTimeout } set { Check(); _recoveryTimeout = value; } }
public OnRecovery : Action[IParseResult] { get { _onRecovery } set { Check(); _onRecovery = value; } }
public DynamicExtensions : Seq[GrammarDescriptor] { get { _dynamicExtensions } set { Check(); _dynamicExtensionsMap = value?.ToDictionary(d => d.FullName); _dynamicExtensions = value; } }
public Statistics : StatisticsTask.Container { get { _statistics } set { Check(); _statistics = value; } }
public CompilerMessages : ICompilerMessages { get { _compilerMessages } set { Check(); _compilerMessages = value; } }
public CurrentGrammar : CompositeGrammar { get; set; }
Check() : void { assert(!_parsingInProgress, "The property can not be changed during parsing."); }
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _startRuleDescriptor : StartRuleDescriptor;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _compositeGrammar : CompositeGrammar;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _completionPrefix : string;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _completionStartPos : int;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _cancellationToken : CancellationToken;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _parseToEndOfString : bool;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _parsingInProgress : bool;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _recoveryTimeout : TimeSpan;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _onRecovery : Action[IParseResult];
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _dynamicExtensions : Seq[GrammarDescriptor];
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _statistics : StatisticsTask.Container;
[DebuggerBrowsable(DebuggerBrowsableState.Never)] mutable _compilerMessages : ICompilerMessages;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal mutable _dynamicExtensionsMap : SCG.Dictionary[string, GrammarDescriptor];
public static SmartRecovery : Action[IParseResult] = parseResult => RecoveryParser(parseResult).RecoveryFromAllErrors();
public static PanicRecovery : Action[IParseResult] = parseResult => RecoveryParser(parseResult).PanicRecovery();
public static FirsrErrorRecovery : Action[IParseResult] = parseResult => RecoveryParser(parseResult).FirstErrorRecovery();
public Parse(sourceText : string) : IParseResult
{
Parse(SourceSnapshot(sourceText))
}
public Parse(sourceSnapshot : SourceSnapshot) : IParseResult
requires this.StartRuleDescriptor != null
{
Parse(ParseResult(this, sourceSnapshot));
}
internal Parse(parseResult : ParseResult) : ParseResult
requires this.StartRuleDescriptor != null
{
when (_onRecovery == null)
_onRecovery = SmartRecovery;
CurrentGrammar = CompositeGrammar;
_parsingInProgress = true;
try
{
parseResult.Parse();
parseResult
}
finally _parsingInProgress = false;
}
}
}
You can’t perform that action at this time.