public CodeFile(CstGenerator cstGenerator, FileInfo info) { Info = info; Code = GuessEncoding.ReadAllText(info.FullName); Ast = cstGenerator.GenerateTreeFromCodeText(Code); Range2Elements = new Dictionary <CodeRange, CstNode>(); }
/// <summary> /// 指定したディレクトリ内のソースコードから統一コードオブジェクトを生成して, ソースコードと統一コードオブジェクトを正常に再生成できるか検査します. /// </summary> /// <param name="dirPath"> 検査対象のソースコードが格納されているディレクトリのパス </param> /// <param name="relativePathsForBinaryFiles">バイナリファイルが存在するディレクトリの相対パスのリスト</param> /// <param name="compileActionByWorkDirPath"> コンパイル処理 </param> public void VerifyRegenerateCodeUsingProject( string dirPath, IList <string> relativePathsForBinaryFiles, Action <string> compileActionByWorkDirPath) { // コンパイル用の作業ディレクトリの取得 var workPath = FixtureUtil.CleanOutputAndGetOutputPath(); // 作業ディレクトリ内にソースコードを配置 FileUtility.CopyRecursively(dirPath, workPath); // 作業ディレクトリ内でコンパイル compileActionByWorkDirPath(workPath); // コンパイル結果の取得 var orgByteCode1 = Fixture.GetAllCompiledCode( workPath, relativePathsForBinaryFiles); var codePaths = Fixture.GetAllSourceFilePaths(workPath); foreach (var codePath in codePaths) { var orgCode1 = GuessEncoding.ReadAllText(codePath); // モデルを生成して,合わせて各種検査を実施する var codeAndObject = GenerateCodeObject(codePath); AssertEqualsModel(orgCode1, codeAndObject.Item2); var code2 = Fixture.CodeGenerator.Generate(codeAndObject.Item2); File.WriteAllText(codePath, code2, XEncoding.SJIS); } // 再生成したソースコードのコンパイル結果の取得 compileActionByWorkDirPath(workPath); var byteCode2 = Fixture.GetAllCompiledCode(workPath, relativePathsForBinaryFiles); AssertFuzzyEquals(byteCode2, orgByteCode1); }
private Tuple <string, UnifiedProgram> GenerateCodeObject(string path) { var code = GuessEncoding.ReadAllText(path); var obj = Fixture.ProgramGenerator.Generate(code); return(Tuple.Create(code, obj)); }
public CodeFile(CodeToXml codeToXml, FileInfo info) { Info = info; Code = GuessEncoding.ReadAllText(info.FullName); Ast = codeToXml.Generate(Code); Range2Elements = new Dictionary <CodeRange, XElement>(); }
/// <summary> /// Generates source code from the specified xml file and encoding. /// </summary> /// <param name="xmlFile"></param> /// <param name="encoding"></param> /// <returns></returns> public string GenerateCodeFromXml(FileInfo xmlFile, Encoding encoding = null) { Contract.Requires(xmlFile != null); Contract.Ensures(Contract.Result <string>() != null); if (encoding == null) { return(GenerateCodeFromXmlText(GuessEncoding.ReadAllText(xmlFile.FullName))); } using (var stream = new StreamReader(xmlFile.FullName, encoding)) { return(GenerateCodeFromXml(stream)); } }
/// <summary> /// Try to parse the fragment of the source code which is retrieved from the specified <c>FileInfo</c>. /// </summary> /// <param name="codeFile"></param> /// <param name="encoding"></param> /// <returns></returns> public void TryParseFromCode(FileInfo codeFile, Encoding encoding = null) { if (encoding == null) { try { TryParseFromCodeText(GuessEncoding.ReadAllText(codeFile.FullName)); } catch { TryParseFromCodeText(File.ReadAllText(codeFile.FullName)); } } else { using (var reader = new StreamReader(codeFile.FullName, encoding)) { TryParseFromCode(reader); } } }
/// <summary> /// Generates a xml from the specified file of the source code and the specified encoding. /// </summary> /// <param name="codeFile"></param> /// <param name="encoding"></param> /// <param name="throwingParseError"></param> /// <returns></returns> public XElement GenerateXmlFromCode( FileInfo codeFile, Encoding encoding = null, bool throwingParseError = DefaultThrowingParseError) { Contract.Requires(codeFile != null); if (encoding == null) { try { return(GenerateXmlFromCodeText( GuessEncoding.ReadAllText(codeFile.FullName), true)); } catch { return(GenerateXmlFromCodeText( File.ReadAllText(codeFile.FullName), throwingParseError)); } } using (var reader = new StreamReader(codeFile.FullName, encoding)) { return(GenerateXmlFromCode(reader, throwingParseError)); } }
public virtual UnifiedProgram GenerateFromFile(string filePath) { var code = GuessEncoding.ReadAllText(filePath); return(Generate(code)); }